鱼C论坛

 找回密码
 立即注册
查看: 104|回复: 1

[已解决]C语言 单链表03

[复制链接]
发表于 2024-4-19 11:18:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

                               
登录/注册后可看大图



  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Dode
  4. {
  5.         int value;
  6.         struct Dode *next;
  7. };

  8. void insertDode(struct Dode **head, int value)
  9. {
  10.         struct Dode *previous;
  11.         struct Dode *current;
  12.         struct Dode *new;

  13.         previous = NULL;
  14.         current = *head;
  15.        
  16.         while(current != NULL && current -> value < value)
  17.         {
  18.                 previous = current;
  19.                 current = current -> next;
  20.         }

  21.         new = (struct Dode *)malloc(sizeof(struct Dode));

  22.         if(new == NULL)
  23.         {
  24.                 printf("内存分配失败!");
  25.                 exit(1);
  26.         }

  27.         new -> value = value;
  28.         new -> next = current;

  29.         if(previous == NULL)
  30.         {
  31.                 *head = new;
  32.         }
  33.         else
  34.         {
  35.                 previous -> next = new;
  36.         }
  37. }

  38. void printDode(struct Dode *head)
  39. {
  40.         struct Dode *current;

  41.         current = head;
  42.         while(current != NULL)
  43.         {
  44.                 printf("%d ", current -> value);
  45.                 current = current -> next;
  46.         }
  47.         putchar('\n');
  48. }

  49. void turnDode(struct Dode **head, int value)
  50. {
  51.         struct Dode *current;
  52.         static struct Dode *tail;

  53.         current = *head;               
  54.         if(*head != NULL)
  55.         {
  56.                 tail -> next = current;
  57.                 current -> next = NULL;
  58.         }
  59.         else
  60.         {
  61.                 *head = current;
  62.                 current -> next = NULL;
  63.         }

  64.         tail = current;

  65.         while(tail != NULL)
  66.         {
  67.                 printf("%d ", tail -> value);
  68.                 tail = tail -> next;
  69.         }
  70. }

  71. int main(void)
  72. {
  73.         struct Dode *head = NULL;
  74.         int input;
  75.        
  76.         printf("开始录入数据到单链表a...\n");

  77.         while(1)
  78.         {
  79.                 printf("请输入一个整数(输入-1表示结束):");
  80.                 scanf("%d", &input);
  81.                 if(input == -1)
  82.                 {
  83.                         break;
  84.                 }
  85.                 insertDode(&head, input);
  86.                 printDode(head);
  87.         }
  88.         putchar('\n');
  89.         printf("下面将单链表a原地反转...\n");
  90.         turnDode(&head, input);
  91.         printDode(head);

  92.         return 0;
  93. }
复制代码


我写的哪里出问题了?

  1. 开始录入数据到单链表a...
  2. 请输入一个整数(输入-1表示结束):1
  3. 1
  4. 请输入一个整数(输入-1表示结束):2
  5. 1 2
  6. 请输入一个整数(输入-1表示结束):3
  7. 1 2 3
  8. 请输入一个整数(输入-1表示结束):-1

  9. 下面将单链表a原地反转...
  10. zsh: bus error  ./a.out
复制代码
最佳答案
2024-4-19 11:19:04
问题出在turnDode函数中的逻辑错误。在反转链表时,应该遍历整个链表,将每个节点的next指针指向其上一个节点,最后将头结点指向最后一个节点。但是你的代码只是将每个节点的next指针都指向了第一个节点,造成了循环链表。

以下是修正后的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node
  4. {
  5.     int value;
  6.     struct Node *next;
  7. };

  8. void insertNode(struct Node **head, int value)
  9. {
  10.     struct Node *previous;
  11.     struct Node *current;
  12.     struct Node *newNode;

  13.     previous = NULL;
  14.     current = *head;

  15.     while (current != NULL && current->value < value)
  16.     {
  17.         previous = current;
  18.         current = current->next;
  19.     }

  20.     newNode = (struct Node *)malloc(sizeof(struct Node));

  21.     if (newNode == NULL)
  22.     {
  23.         printf("内存分配失败!\n");
  24.         exit(1);
  25.     }

  26.     newNode->value = value;
  27.     newNode->next = current;

  28.     if (previous == NULL)
  29.     {
  30.         *head = newNode;
  31.     }
  32.     else
  33.     {
  34.         previous->next = newNode;
  35.     }
  36. }

  37. void printList(struct Node *head)
  38. {
  39.     struct Node *current = head;

  40.     while (current != NULL)
  41.     {
  42.         printf("%d ", current->value);
  43.         current = current->next;
  44.     }
  45.     printf("\n");
  46. }

  47. void reverseList(struct Node** head)
  48. {
  49.     struct Node* previous = NULL;
  50.     struct Node* current = *head;
  51.     struct Node* next = NULL;

  52.     while (current != NULL) {
  53.         // 保存下一个节点
  54.         next = current->next;
  55.       
  56.         // 反转节点的指针
  57.         current->next = previous;
  58.       
  59.         // 更新指针位置
  60.         previous = current;
  61.         current = next;
  62.     }
  63.   
  64.     // 更新头指针
  65.     *head = previous;
  66. }

  67. int main(void)
  68. {
  69.     struct Node *head = NULL;
  70.     int input;

  71.     printf("开始录入数据到单链表...\n");

  72.     while (1)
  73.     {
  74.         printf("请输入一个整数(输入-1表示结束):");
  75.         scanf("%d", &input);
  76.         if (input == -1)
  77.         {
  78.             break;
  79.         }
  80.         insertNode(&head, input);
  81.         printList(head);
  82.     }
  83.     printf("\n");

  84.     printf("下面将单链表反转...\n");
  85.     reverseList(&head);
  86.     printList(head);

  87.     return 0;
  88. }
复制代码


这样就可以正确地实现单链表的插入和反转功能了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-19 11:19:04 | 显示全部楼层    本楼为最佳答案   
问题出在turnDode函数中的逻辑错误。在反转链表时,应该遍历整个链表,将每个节点的next指针指向其上一个节点,最后将头结点指向最后一个节点。但是你的代码只是将每个节点的next指针都指向了第一个节点,造成了循环链表。

以下是修正后的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node
  4. {
  5.     int value;
  6.     struct Node *next;
  7. };

  8. void insertNode(struct Node **head, int value)
  9. {
  10.     struct Node *previous;
  11.     struct Node *current;
  12.     struct Node *newNode;

  13.     previous = NULL;
  14.     current = *head;

  15.     while (current != NULL && current->value < value)
  16.     {
  17.         previous = current;
  18.         current = current->next;
  19.     }

  20.     newNode = (struct Node *)malloc(sizeof(struct Node));

  21.     if (newNode == NULL)
  22.     {
  23.         printf("内存分配失败!\n");
  24.         exit(1);
  25.     }

  26.     newNode->value = value;
  27.     newNode->next = current;

  28.     if (previous == NULL)
  29.     {
  30.         *head = newNode;
  31.     }
  32.     else
  33.     {
  34.         previous->next = newNode;
  35.     }
  36. }

  37. void printList(struct Node *head)
  38. {
  39.     struct Node *current = head;

  40.     while (current != NULL)
  41.     {
  42.         printf("%d ", current->value);
  43.         current = current->next;
  44.     }
  45.     printf("\n");
  46. }

  47. void reverseList(struct Node** head)
  48. {
  49.     struct Node* previous = NULL;
  50.     struct Node* current = *head;
  51.     struct Node* next = NULL;

  52.     while (current != NULL) {
  53.         // 保存下一个节点
  54.         next = current->next;
  55.       
  56.         // 反转节点的指针
  57.         current->next = previous;
  58.       
  59.         // 更新指针位置
  60.         previous = current;
  61.         current = next;
  62.     }
  63.   
  64.     // 更新头指针
  65.     *head = previous;
  66. }

  67. int main(void)
  68. {
  69.     struct Node *head = NULL;
  70.     int input;

  71.     printf("开始录入数据到单链表...\n");

  72.     while (1)
  73.     {
  74.         printf("请输入一个整数(输入-1表示结束):");
  75.         scanf("%d", &input);
  76.         if (input == -1)
  77.         {
  78.             break;
  79.         }
  80.         insertNode(&head, input);
  81.         printList(head);
  82.     }
  83.     printf("\n");

  84.     printf("下面将单链表反转...\n");
  85.     reverseList(&head);
  86.     printList(head);

  87.     return 0;
  88. }
复制代码


这样就可以正确地实现单链表的插入和反转功能了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 11:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表