鱼C论坛

 找回密码
 立即注册
查看: 1124|回复: 3

关于链表

[复制链接]
发表于 2023-10-25 16:34:45 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 w349695434051 于 2023-10-26 18:33 编辑
  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语句就死循环了
链表这几天刚学的
求大佬指点一下
有别的问题也劳烦修改一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-25 16:35:24 | 显示全部楼层
问题出在list_retrieval函数中的for循环中,循环条件应该是 rep != NULL 而不是 rep 。修改后的代码如下:

  1. int list_retrieval(List* list)
  2. {
  3.     int ret = 0;
  4.     int reindex = 0;
  5.     Node* rep = NULL;
  6.     if(list -> head)
  7.     {
  8.         ret = 1;
  9.         for(rep = list -> head; rep != NULL; rep = rep -> next)
  10.         {
  11.             rep -> index = reindex++;
  12.         }
  13.     }
  14.     return ret;
  15. }
复制代码


此外,list_insert函数中的 p -> next == NULL 应该修改为 p -> next = NULL ,list_delete函数中的 list_retrieval; 应该修改为 list_retrieval(list); 。

修改后的代码如下:

  1. Node* list_insert(List* list, int index, int number)
  2. {
  3.     Node* p = NULL;
  4.    
  5.     if(index <= list -> number && index >= 0)
  6.     {
  7.         p = (Node*)malloc(sizeof(Node));
  8.         p -> value = number;
  9.         p -> next = NULL;
  10.         
  11.         if(index <= list -> number && index)
  12.             list_at(list, index - 1) -> next = p;
  13.         else if(index == 0)
  14.             list -> head = p;
  15.         
  16.         if(index == list -> number)
  17.             p -> next = NULL;
  18.         else
  19.             p -> next = list_at(list, index);
  20.         
  21.         list_retrieval(list);
  22.     }
  23. }

  24. int list_delete(List* list, int index)
  25. {
  26.     int ret = 0;
  27.     if(list -> head && list -> number > index && index >= 0)
  28.     {
  29.         ret = 1;
  30.         Node* p = NULL;
  31.         if(index > 0)
  32.             list_at(list, index - 1) -> next = list_at(list, index)-> next;
  33.         else
  34.             list -> head = list_at(list, index) -> next;
  35.         
  36.         free(p);
  37.         list_retrieval(list);
  38.     }
  39.     return ret;
  40. }
复制代码


修改后的代码应该可以正常运行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-25 17:04:39 | 显示全部楼层
isdkz 发表于 2023-10-25 16:35
问题出在list_retrieval函数中的for循环中,循环条件应该是 rep != NULL 而不是 rep 。修改后的代码如下:
...

其他两处错已经改了
但改成 !=NULL 以后还是死循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-25 20:24:22 | 显示全部楼层
w349695434051 发表于 2023-10-25 17:04
其他两处错已经改了
但改成 !=NULL 以后还是死循环

他是脚本GPT回答,你这里rep和rep!=NULL就是一样的……我没有仔细看(没有那么多时间),只注意了你的list_retrieval函数,逻辑上说应该没有问题,有可能你的链表初始化或者在插入删除操作中没有注意最后一个要指向NULL,我建议你可以将其改写为while循环并且设置循环退出条件为rep == NULL来测试一下是不是最后一个没有指向NULL的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 01:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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