23-4.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. struct Toy
  4. {
  5. int val; // 玩具的编号
  6. struct Toy *next;
  7. };
  8. void print(struct Toy *head){
  9. struct Toy *temp = head->next;
  10. while(temp != NULL){
  11. printf("%d-",temp->val);
  12. temp = temp->next;
  13. }
  14. printf("\n");
  15. }
  16. struct Toy *createToy(int val){
  17. struct Toy *t = (struct Toy *)malloc(sizeof(struct Toy));
  18. if(t == NULL){
  19. return NULL;
  20. }
  21. t->val = val;
  22. return t;
  23. }
  24. bool init(struct Toy *head){
  25. struct Toy *temp = head;
  26. for(int i = 1;i<=10;i++){
  27. temp->next = createToy(i);
  28. temp = temp->next;
  29. if(temp == NULL){
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. void game(struct Toy *head,int val){
  36. struct Toy *temp = head;
  37. while (temp->next != NULL)
  38. {
  39. if(temp->next->val == val){
  40. break;
  41. }
  42. temp = temp->next;
  43. }
  44. struct Toy *t = temp->next;
  45. temp->next = t->next;
  46. t->next = head->next;
  47. head->next = t;
  48. }
  49. int main(){
  50. struct Toy *head = createToy(-1); //头节点
  51. if(head == NULL){
  52. printf("malloc fail\n");
  53. return -1;
  54. }
  55. bool in = init(head);
  56. if(in == false){
  57. printf("init fail\n");
  58. return -1;
  59. }
  60. print(head);
  61. game(head,3); // 骰子数为3
  62. print(head);
  63. return 0;
  64. }
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。