鱼C论坛

 找回密码
 立即注册
查看: 1901|回复: 2

[已解决]自己写的链表有点问题

[复制链接]
发表于 2023-10-26 18:49:57 | 显示全部楼层 |阅读模式

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

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

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

  3. typedef struct _node{                                                                        //定义链表节点
  4.         int value;                                                                                        //每个节点中包含两个元素 他存储的值value 还有便于检索的下标index
  5.         int index;
  6.         struct _node* next;
  7. }Node;

  8. typedef struct _list{                                                                        //定义链表
  9.         int number;                                                                                        //每个链表包含number 代表他一共含有几个节点
  10.         Node* head;                                                                                        //链表的头
  11.         Node* tail;                                                                                        //链表的tail 指向链表最后一个节点 用来延长链表的
  12. }List;

  13. Node* list_add(List* list, int number);                                        //定义链表延长函数
  14. int list_print(List *list);                                                                //链表输出节点的值的函数
  15. Node* list_at(List* list, int index);                                        //根据下标定位节点的函数
  16. Node* list_insert(List* list, int index, int number);        //再链表中插入结点的函数
  17. int list_retrieval(List* list);                                                        //插入或者删除节点后 对链表节点重新排下标的函数
  18. int list_delete(List* list, int index);                                        //输出链表结点的函数
  19. Node* list_found(List* list, int number);                                //根据链表的值输出节点地址的函数
  20. void list_free(List* list);                                                                //free链表的函数

  21. Node* list_add(List* list, int number)
  22. {
  23.         Node* p = (Node*)malloc(sizeof(Node));
  24.         p -> value = number;
  25.         p -> next = NULL;
  26.         if(list -> head)                                                                        //判断链表是否为空链表
  27.         {                                                                                                
  28.                 list -> tail -> next = p;                                                //不是的话让指向最后一个节点的tail的next指向新定义的节点
  29.                 p -> index = list -> tail -> index + 1;                        //他的下标就是最后节点下标加一
  30.                 list -> number++;                                                                //链表节点数量加一
  31.         }else                                                                                                        
  32.         {                                                                                                        //链表是空链表
  33.                 list -> head = p;                                                                //链表头指向新定义的节点
  34.                 p -> index = 0;                                                                        //下标为零
  35.                 list -> number = 1;                                                                //节点数量初始化为一
  36.         }
  37.         list -> tail = p;                                                                        //最后让tail指向新定义的节点 作为链表最后一个节点
  38. }

  39. int list_print(List *list)
  40. {
  41.         int ret = 0;
  42.         if(list -> head)                                                                        //打印函数 如果为空链表不调用函数返回值为零
  43.         {
  44.                 ret = 1;
  45.                 Node *p = NULL;
  46.                 for(p = list -> head; p; p = p -> next)
  47.                 {
  48.                         printf("%d", p -> value);
  49.                         if(p -> next)                                                                //链表除了最后一个节点都打印一个tab
  50.                                 printf("\t");
  51.                 }
  52.                 printf("\n");
  53.         }
  54.         return ret;
  55. }

  56. Node* list_at(List* list, int index)
  57. {
  58.         Node* p = NULL;
  59.         if(index < list -> number && index >= 0)                        //输入有效下表进入函数 否则返回零地址
  60.                 for(p = list -> head; p; p = p -> next)
  61.                         if(p -> index == index)
  62.                                 break;
  63.         return p;
  64. }

  65. Node* list_insert(List* list, int index, int number)        //链表插入函数
  66. {
  67.         Node* p = NULL;
  68.         
  69.         if(index <= list -> number && index >= 0)                        //判断述如下表是否有效
  70.         {
  71.                 p = (Node*)malloc(sizeof(Node));
  72.                 p -> value = number;
  73.                 p -> next = NULL;
  74.                
  75.                 if(index <= list -> number && index)                        //当插入的位置 不为第一个时
  76.                         list_at(list, index - 1) -> next = p;                //调用取址函数让插入的前一个节点指向新插入的节点
  77.                 else if(index == 0)                                                                //当插入位置 为第一个时
  78.                         list -> head = p;                                                        //链表头指向新节点
  79.                         
  80.                 if(index == list -> number)                                                //当插入位置为最后一个 相当于延长了一个节点
  81.                         p -> next = NULL;                                                        //新节点next指向null
  82.                 else
  83.                         p -> next = list_at(list, index);                        //否则的话 next指向被插入的节点
  84.                         
  85.                 list_retrieval(list);                                                        //调用排序函数对链表下标在排序
  86.         }
  87. }

  88. int list_retrieval(List* list)
  89. {
  90.         int ret = 0;
  91.         int reindex = 0;
  92.         Node* rep = NULL;
  93.         if(list -> head)
  94.         {
  95.                 ret = 1;
  96.                 for(rep = list -> head; rep != NULL; rep = rep -> next)                //重点就是这个排序函数 进到for循环之后就死循环了 但我看着也没有问题啊 !!
  97.                 {
  98.                         int a;
  99.                         rep -> index = reindex++;
  100.                 }
  101.         }
  102.         return ret;
  103. }

  104. int list_delete(List* list, int index)                                                //节点删除函数
  105. {
  106.         int ret = 0;
  107.         if(list -> head && list -> number > index && index >= 0)        //如果链表为空链表并且下标无效的话不调用函数
  108.         {
  109.                 ret = 1;
  110.                 Node* p = NULL;
  111.                 if(index > 0)                                                                                //删除的节点不是第一个节点
  112.                         list_at(list, index - 1) -> next =  list_at(list, index)-> next;        //调用取址函数
  113.                 else
  114.                         list -> head = list_at(list, index) -> next;        //删除的是第一个节点
  115.                 free(p);
  116.                 list_retrieval(list);                                                                                //对删除之后的链表下标排序
  117.         }
  118.         return ret;
  119. }

  120. Node* list_found(List* list, int number)                                        //根据链表的值输出节点地址的函数
  121. {
  122.         Node* p = NULL;
  123.         for(p = list -> head; p; p = p -> next)
  124.                 if(p -> value == number)
  125.                         break;
  126.         return p;
  127. }

  128. void list_free(List* list)                                                                        //free链表的函数
  129. {
  130.         Node* p = list -> head;
  131.         Node* q = NULL;
  132.         while(p)
  133.         {
  134.                 q = p -> next;
  135.                 free(p);
  136.                 p = q;
  137.         }
  138. }

  139. int main()
  140. {
  141.         List list;
  142.         list.head = NULL;
  143.         list.tail = NULL;
  144.         
  145.         list_add(&list, 1);
  146.         list_add(&list, 2);
  147.         list_add(&list, 3);
  148.         
  149.         list_print(&list);
  150.         
  151.         list_insert(&list, 0, 4);
  152.         list_insert(&list, 4, 5);
  153.         
  154.         list_delete(&list, 0);
  155.         list_print(&list);
  156.         list_delete(&list, 1);
  157.         list_print(&list);
  158.         list_delete(&list, 0);
  159.         list_print(&list);
  160.         
  161.         list_print(&list);
  162.         
  163.         list_free(&list);
  164.         
  165.         return 0;
  166. }
复制代码
调用insert函数还有delete函数
进入retrieval的for语句就死循环了
链表这几天刚学的
求大佬指点一下
我看着逻辑没啥问题但怎么改都是死循环
真是要崩溃了
求助
最佳答案
2023-12-1 05:32:16
小甲鱼的二师兄 发表于 2023-12-01 05:31
#include
#include

typedef struct _node{      ...

你的代码中,在排序函数 `list_retrieval()` 的 for 循环中,有一个问题导致了死循环。问题出在循环条件上(`rep != NULL`),你使用了中文的感叹号,但实际应该使用英文的感叹号 `!=` 。

修复这个问题后,可以将循环条件修改为 `rep != NULL`,就可以避免死循环了。这样,每次循环时,`rep` 都会指向下一个节点,直到链表末尾为止。

同时,我还注意到你在删除节点的代码中,没有正确释放内存。在 `list_delete()` 函数中,你定义了一个指针 `p` 来指向要删除的节点,但在释放内存之前,你没有将 `p` 更新为要删除节点的下一个节点,导致内存泄漏。

以下是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct _node {
  4.     int value;
  5.     int index;
  6.     struct _node* next;
  7. } Node;
  8. typedef struct _list {
  9.     int number;
  10.     Node* head;
  11.     Node* tail;
  12. } List;
  13. Node* list_add(List* list, int number);
  14. int list_print(List *list);
  15. Node* list_at(List* list, int index);
  16. Node* list_insert(List* list, int index, int number);
  17. int list_retrieval(List* list);
  18. int list_delete(List* list, int index);
  19. Node* list_found(List* list, int number);
  20. void list_free(List* list);
  21. Node* list_add(List* list, int number) {
  22.     Node* p = (Node*)malloc(sizeof(Node));
  23.     p->value = number;
  24.     p->next = NULL;
  25.     if (list->head) {
  26.         list->tail->next = p;
  27.         p->index = list->tail->index + 1;
  28.         list->number++;
  29.     } else {
  30.         list->head = p;
  31.         p->index = 0;
  32.         list->number = 1;
  33.     }
  34.     list->tail = p;
  35.     return p;
  36. }
  37. int list_print(List *list) {
  38.     if (!list->head) {
  39.         return 0;
  40.     }
  41.     Node *p = NULL;
  42.     for (p = list->head; p; p = p->next) {
  43.         printf("%d", p->value);
  44.         if (p->next) {
  45.             printf("\t");
  46.         }
  47.     }
  48.     printf("\n");
  49.     return 1;
  50. }
  51. Node* list_at(List* list, int index) {
  52.     Node* p = NULL;
  53.     if (index < list->number && index >= 0) {
  54.         for (p = list->head; p; p = p->next) {
  55.             if (p->index == index) {
  56.                 break;
  57.             }
  58.         }
  59.     }
  60.     return p;
  61. }
  62. Node* list_insert(List* list, int index, int number) {
  63.     if (index < 0 || index > list->number) {
  64.         return NULL; // 非法的下标
  65.     }
  66.     Node* p = (Node*)malloc(sizeof(Node));
  67.     p->value = number;
  68.     if (index == 0) {
  69.         p->next = list->head;
  70.         list->head = p;
  71.         if (list->number == 0) {
  72.             list->tail = p;
  73.         }
  74.     } else {
  75.         Node* prev = list_at(list, index-1);
  76.         p->next = prev->next;
  77.         prev->next = p;
  78.         if (index == list->number) {
  79.             list->tail = p;
  80.         }
  81.     }
  82.     list_retrieval(list);
  83.     return p;
  84. }
  85. int list_retrieval(List* list) {
  86.     if (!list->head) {
  87.         return 0;
  88.     }
  89.     int reindex = 0;
  90.     Node* rep = NULL;
  91.     for (rep = list->head; rep != NULL; rep = rep->next) {
  92.         rep->index = reindex++;
  93.     }
  94.     return 1;
  95. }
  96. int list_delete(List* list, int index) {
  97.     if (!list->head || index < 0 || index >= list->number) {
  98.         return 0;
  99.     }
  100.     Node* p = NULL;
  101.     if (index == 0) {
  102.         p = list->head;
  103.         list->head = list->head->next;
  104.         if (list->number == 1) {
  105.             list->tail = NULL;
  106.         }
  107.     } else {
  108.         Node* prev = list_at(list, index-1);
  109.         p = prev->next;
  110.         prev->next = p->next;
  111.         if (index == list->number - 1) {
  112.             list->tail = prev;
  113.         }
  114.     }
  115.     free(p);
  116.     list_retrieval(list);
  117.     return 1;
  118. }
  119. Node* list_found(List* list, int number) {
  120.     Node* p = NULL;
  121.     for (p = list->head; p; p = p->next) {
  122.         if (p->value == number) {
  123.             break;
  124.         }
  125.     }
  126.     return p;
  127. }
  128. void list_free(List* list) {
  129.     Node* p = list->head;
  130.     Node* q = NULL;
  131.     while (p) {
  132.         q = p->next;
  133.         free(p);
  134.         p = q;
  135.     }
  136. }
  137. int main() {
  138.     List list;
  139.     list.head = NULL;
  140.     list.tail = NULL;
  141.    
  142.     list_add(&list, 1);
  143.     list_add(&list, 2);
  144.     list_add(&list, 3);
  145.     list_print(&list);
  146.    
  147.     list_insert(&list, 0, 4);
  148.     list_insert(&list, 4, 5);
  149.    
  150.     list_delete(&list, 0);
  151.     list_print(&list);
  152.     list_delete(&list, 1);
  153.     list_print(&list);
  154.     list_delete(&list, 0);
  155.     list_print(&list);
  156.     list_free(&list);
  157.     return 0;
  158. }
复制代码

现在,你的插入和删除操作应该能够正常运行,并且循环也会正确退出。希望这对你有帮助!如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-1 05:31:41 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct _node{                                                                        //定义链表节点
  4.         int value;                                                                                        //每个节点中包含两个元素 他存储的值value 还有便于检索的下标index
  5.         int index;
  6.         struct _node* next;
  7. }Node;

  8. typedef struct _list{                                                                        //定义链表
  9.         int number;                                                                                        //每个链表包含number 代表他一共含有几个节点
  10.         Node* head;                                                                                        //链表的头
  11.         Node* tail;                                                                                        //链表的tail 指向链表最后一个节点 用来延长链表的
  12. }List;

  13. Node* list_add(List* list, int number);                                        //定义链表延长函数
  14. int list_print(List *list);                                                                //链表输出节点的值的函数
  15. Node* list_at(List* list, int index);                                        //根据下标定位节点的函数
  16. Node* list_insert(List* list, int index, int number);        //再链表中插入结点的函数
  17. int list_retrieval(List* list);                                                        //插入或者删除节点后 对链表节点重新排下标的函数
  18. int list_delete(List* list, int index);                                        //输出链表结点的函数
  19. Node* list_found(List* list, int number);                                //根据链表的值输出节点地址的函数
  20. void list_free(List* list);                                                                //free链表的函数

  21. Node* list_add(List* list, int number)
  22. {
  23.         Node* p = (Node*)malloc(sizeof(Node));
  24.         p -> value = number;
  25.         p -> next = NULL;
  26.         if(list -> head)                                                                        //判断链表是否为空链表
  27.         {                                                                                                
  28.                 list -> tail -> next = p;                                                //不是的话让指向最后一个节点的tail的next指向新定义的节点
  29.                 p -> index = list -> tail -> index + 1;                        //他的下标就是最后节点下标加一
  30.                 list -> number++;                                                                //链表节点数量加一
  31.         }else                                                                                                        
  32.         {                                                                                                        //链表是空链表
  33.                 list -> head = p;                                                                //链表头指向新定义的节点
  34.                 p -> index = 0;                                                                        //下标为零
  35.                 list -> number = 1;                                                                //节点数量初始化为一
  36.         }
  37.         list -> tail = p;                                                                        //最后让tail指向新定义的节点 作为链表最后一个节点
  38. }

  39. int list_print(List *list)
  40. {
  41.         int ret = 0;
  42.         if(list -> head)                                                                        //打印函数 如果为空链表不调用函数返回值为零
  43.         {
  44.                 ret = 1;
  45.                 Node *p = NULL;
  46.                 for(p = list -> head; p; p = p -> next)
  47.                 {
  48.                         printf("%d", p -> value);
  49.                         if(p -> next)                                                                //链表除了最后一个节点都打印一个tab
  50.                                 printf("\t");
  51.                 }
  52.                 printf("\n");
  53.         }
  54.         return ret;
  55. }

  56. Node* list_at(List* list, int index)
  57. {
  58.         Node* p = NULL;
  59.         if(index < list -> number && index >= 0)                        //输入有效下表进入函数 否则返回零地址
  60.                 for(p = list -> head; p; p = p -> next)
  61.                         if(p -> index == index)
  62.                                 break;
  63.         return p;
  64. }

  65. Node* list_insert(List* list, int index, int number)        //链表插入函数
  66. {
  67.         Node* p = NULL;
  68.         
  69.         if(index <= list -> number && index >= 0)                        //判断述如下表是否有效
  70.         {
  71.                 p = (Node*)malloc(sizeof(Node));
  72.                 p -> value = number;
  73.                 p -> next = NULL;
  74.                
  75.                 if(index <= list -> number && index)                        //当插入的位置 不为第一个时
  76.                         list_at(list, index - 1) -> next = p;                //调用取址函数让插入的前一个节点指向新插入的节点
  77.                 else if(index == 0)                                                                //当插入位置 为第一个时
  78.                         list -> head = p;                                                        //链表头指向新节点
  79.                         
  80.                 if(index == list -> number)                                                //当插入位置为最后一个 相当于延长了一个节点
  81.                         p -> next = NULL;                                                        //新节点next指向null
  82.                 else
  83.                         p -> next = list_at(list, index);                        //否则的话 next指向被插入的节点
  84.                         
  85.                 list_retrieval(list);                                                        //调用排序函数对链表下标在排序
  86.         }
  87. }

  88. int list_retrieval(List* list)
  89. {
  90.         int ret = 0;
  91.         int reindex = 0;
  92.         Node* rep = NULL;
  93.         if(list -> head)
  94.         {
  95.                 ret = 1;
  96.                 for(rep = list -> head; rep != NULL; rep = rep -> next)                //重点就是这个排序函数 进到for循环之后就死循环了 但我看着也没有问题啊 !!
  97.                 {
  98.                         int a;
  99.                         rep -> index = reindex++;
  100.                 }
  101.         }
  102.         return ret;
  103. }

  104. int list_delete(List* list, int index)                                                //节点删除函数
  105. {
  106.         int ret = 0;
  107.         if(list -> head && list -> number > index && index >= 0)        //如果链表为空链表并且下标无效的话不调用函数
  108.         {
  109.                 ret = 1;
  110.                 Node* p = NULL;
  111.                 if(index > 0)                                                                                //删除的节点不是第一个节点
  112.                         list_at(list, index - 1) -> next =  list_at(list, index)-> next;        //调用取址函数
  113.                 else
  114.                         list -> head = list_at(list, index) -> next;        //删除的是第一个节点
  115.                 free(p);
  116.                 list_retrieval(list);                                                                                //对删除之后的链表下标排序
  117.         }
  118.         return ret;
  119. }

  120. Node* list_found(List* list, int number)                                        //根据链表的值输出节点地址的函数
  121. {
  122.         Node* p = NULL;
  123.         for(p = list -> head; p; p = p -> next)
  124.                 if(p -> value == number)
  125.                         break;
  126.         return p;
  127. }

  128. void list_free(List* list)                                                                        //free链表的函数
  129. {
  130.         Node* p = list -> head;
  131.         Node* q = NULL;
  132.         while(p)
  133.         {
  134.                 q = p -> next;
  135.                 free(p);
  136.                 p = q;
  137.         }
  138. }

  139. int main()
  140. {
  141.         List list;
  142.         list.head = NULL;
  143.         list.tail = NULL;
  144.         
  145.         list_add(&list, 1);
  146.         list_add(&list, 2);
  147.         list_add(&list, 3);
  148.         
  149.         list_print(&list);
  150.         
  151.         list_insert(&list, 0, 4);
  152.         list_insert(&list, 4, 5);
  153.         
  154.         list_delete(&list, 0);
  155.         list_print(&list);
  156.         list_delete(&list, 1);
  157.         list_print(&list);
  158.         list_delete(&list, 0);
  159.         list_print(&list);
  160.         
  161.         list_print(&list);
  162.         
  163.         list_free(&list);
  164.         
  165.         return 0;
  166. }
复制代码

调用insert函数还有delete函数
进入retrieval的for语句就死循环了
@FishC_GPT
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-1 05:32:16 | 显示全部楼层    本楼为最佳答案   
小甲鱼的二师兄 发表于 2023-12-01 05:31
#include
#include

typedef struct _node{      ...

你的代码中,在排序函数 `list_retrieval()` 的 for 循环中,有一个问题导致了死循环。问题出在循环条件上(`rep != NULL`),你使用了中文的感叹号,但实际应该使用英文的感叹号 `!=` 。

修复这个问题后,可以将循环条件修改为 `rep != NULL`,就可以避免死循环了。这样,每次循环时,`rep` 都会指向下一个节点,直到链表末尾为止。

同时,我还注意到你在删除节点的代码中,没有正确释放内存。在 `list_delete()` 函数中,你定义了一个指针 `p` 来指向要删除的节点,但在释放内存之前,你没有将 `p` 更新为要删除节点的下一个节点,导致内存泄漏。

以下是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct _node {
  4.     int value;
  5.     int index;
  6.     struct _node* next;
  7. } Node;
  8. typedef struct _list {
  9.     int number;
  10.     Node* head;
  11.     Node* tail;
  12. } List;
  13. Node* list_add(List* list, int number);
  14. int list_print(List *list);
  15. Node* list_at(List* list, int index);
  16. Node* list_insert(List* list, int index, int number);
  17. int list_retrieval(List* list);
  18. int list_delete(List* list, int index);
  19. Node* list_found(List* list, int number);
  20. void list_free(List* list);
  21. Node* list_add(List* list, int number) {
  22.     Node* p = (Node*)malloc(sizeof(Node));
  23.     p->value = number;
  24.     p->next = NULL;
  25.     if (list->head) {
  26.         list->tail->next = p;
  27.         p->index = list->tail->index + 1;
  28.         list->number++;
  29.     } else {
  30.         list->head = p;
  31.         p->index = 0;
  32.         list->number = 1;
  33.     }
  34.     list->tail = p;
  35.     return p;
  36. }
  37. int list_print(List *list) {
  38.     if (!list->head) {
  39.         return 0;
  40.     }
  41.     Node *p = NULL;
  42.     for (p = list->head; p; p = p->next) {
  43.         printf("%d", p->value);
  44.         if (p->next) {
  45.             printf("\t");
  46.         }
  47.     }
  48.     printf("\n");
  49.     return 1;
  50. }
  51. Node* list_at(List* list, int index) {
  52.     Node* p = NULL;
  53.     if (index < list->number && index >= 0) {
  54.         for (p = list->head; p; p = p->next) {
  55.             if (p->index == index) {
  56.                 break;
  57.             }
  58.         }
  59.     }
  60.     return p;
  61. }
  62. Node* list_insert(List* list, int index, int number) {
  63.     if (index < 0 || index > list->number) {
  64.         return NULL; // 非法的下标
  65.     }
  66.     Node* p = (Node*)malloc(sizeof(Node));
  67.     p->value = number;
  68.     if (index == 0) {
  69.         p->next = list->head;
  70.         list->head = p;
  71.         if (list->number == 0) {
  72.             list->tail = p;
  73.         }
  74.     } else {
  75.         Node* prev = list_at(list, index-1);
  76.         p->next = prev->next;
  77.         prev->next = p;
  78.         if (index == list->number) {
  79.             list->tail = p;
  80.         }
  81.     }
  82.     list_retrieval(list);
  83.     return p;
  84. }
  85. int list_retrieval(List* list) {
  86.     if (!list->head) {
  87.         return 0;
  88.     }
  89.     int reindex = 0;
  90.     Node* rep = NULL;
  91.     for (rep = list->head; rep != NULL; rep = rep->next) {
  92.         rep->index = reindex++;
  93.     }
  94.     return 1;
  95. }
  96. int list_delete(List* list, int index) {
  97.     if (!list->head || index < 0 || index >= list->number) {
  98.         return 0;
  99.     }
  100.     Node* p = NULL;
  101.     if (index == 0) {
  102.         p = list->head;
  103.         list->head = list->head->next;
  104.         if (list->number == 1) {
  105.             list->tail = NULL;
  106.         }
  107.     } else {
  108.         Node* prev = list_at(list, index-1);
  109.         p = prev->next;
  110.         prev->next = p->next;
  111.         if (index == list->number - 1) {
  112.             list->tail = prev;
  113.         }
  114.     }
  115.     free(p);
  116.     list_retrieval(list);
  117.     return 1;
  118. }
  119. Node* list_found(List* list, int number) {
  120.     Node* p = NULL;
  121.     for (p = list->head; p; p = p->next) {
  122.         if (p->value == number) {
  123.             break;
  124.         }
  125.     }
  126.     return p;
  127. }
  128. void list_free(List* list) {
  129.     Node* p = list->head;
  130.     Node* q = NULL;
  131.     while (p) {
  132.         q = p->next;
  133.         free(p);
  134.         p = q;
  135.     }
  136. }
  137. int main() {
  138.     List list;
  139.     list.head = NULL;
  140.     list.tail = NULL;
  141.    
  142.     list_add(&list, 1);
  143.     list_add(&list, 2);
  144.     list_add(&list, 3);
  145.     list_print(&list);
  146.    
  147.     list_insert(&list, 0, 4);
  148.     list_insert(&list, 4, 5);
  149.    
  150.     list_delete(&list, 0);
  151.     list_print(&list);
  152.     list_delete(&list, 1);
  153.     list_print(&list);
  154.     list_delete(&list, 0);
  155.     list_print(&list);
  156.     list_free(&list);
  157.     return 0;
  158. }
复制代码

现在,你的插入和删除操作应该能够正常运行,并且循环也会正确退出。希望这对你有帮助!如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 08:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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