鱼C论坛

 找回密码
 立即注册
查看: 1801|回复: 5

[已解决]帮我捉一下虫子(关于c 链表部分)

[复制链接]
发表于 2022-5-10 12:32:30 | 显示全部楼层 |阅读模式

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

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

x
看一下输出结果
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107   106   105
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107   106   105
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107   106   105
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107   106   105   104   103   102   101   100

PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107   106
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10
109   108   107   106
PS D:\001> ./w4
  19    18    17    16    15    14    13    12    11    10  
109   108   107   106   105   104   103   102   101   100

PS D:\001>

红色部分为正常输出(期待输出)  其余的 都是 有问题的,明显是指针飞了,可是不知道哪里错了呢
代码很简单,创建2个链表,分别输出这两个链表
代码如下:
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=NULL;
  12.     for(int x=10;x<20;x++)
  13.     {
  14.         struct Node * node1=NULL;
  15.         node1=(struct Node*)malloc(sizeof(struct Node*));
  16.         node1->data=x;
  17.         node1->next=head;
  18.         head=node1;
  19.     }
  20.     return head;
  21. }

  22. //带头节点的 头插法
  23. struct Node * init_link2(struct Node * s)
  24. {
  25.     struct Node * head=NULL;
  26.     for(int x=100;x<110;x++)
  27.     {
  28.         struct Node * node1=NULL;
  29.         node1=(struct Node*)malloc(sizeof(struct Node*));
  30.         node1->data=x;
  31.         node1->next=head;
  32.         head=node1;
  33.     }
  34.     return head;
  35. }


  36. int main()
  37. {
  38.         struct Node * head=NULL;
  39.         head=(struct Node*)malloc(sizeof(struct Node*));
  40.         head->data=0;
  41.         head->next=NULL;
  42.         //输出链表一
  43.         struct Node *  pmove=init_link(head);
  44.         while (pmove)
  45.         {
  46.                 printf("%4d  ",pmove->data);
  47.                 if(pmove->next==NULL) break;     
  48.                 pmove=pmove->next;
  49.         }
  50.         printf("\n");
  51.         //输出链表二
  52.         struct Node * head2=NULL;
  53.         head2=(struct Node*)malloc(sizeof(struct Node*));
  54.         head2->data=0;
  55.         head2->next=NULL;
  56.         struct Node *  pmove2=init_link2(head2);
  57.         while (pmove2)
  58.         {
  59.              printf("%4d  ",pmove2->data);
  60.              if(pmove2->next==NULL) break;     
  61.              pmove2=pmove2->next;
  62.         }
  63.         printf("\n");
  64.         return 0;
  65. }

复制代码
最佳答案
2022-5-10 13:01:37
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=NULL;
  12.     for(int x=10;x<20;x++)
  13.     {
  14.         struct Node * node1=NULL;
  15.         node1=(struct Node*)malloc(sizeof(struct Node));/////////////////////////////////////////////是结构体的大小,不是结构体指针的大小
  16.         node1->data=x;
  17.         node1->next=head;
  18.         head=node1;
  19.     }
  20.     return head;
  21. }

  22. //带头节点的 头插法
  23. struct Node * init_link2(struct Node * s)
  24. {
  25.     struct Node * head=NULL;
  26.     for(int x=100;x<110;x++)
  27.     {
  28.         struct Node * node1=NULL;
  29.         node1=(struct Node*)malloc(sizeof(struct Node));////////////////////////////////////
  30.         node1->data=x;
  31.         node1->next=head;
  32.         head=node1;
  33.     }
  34.     return head;
  35. }


  36. int main()
  37. {
  38.         struct Node * head=NULL;
  39.         head=(struct Node*)malloc(sizeof(struct Node));///////////////////////////
  40.         head->data=0;
  41.         head->next=NULL;
  42.         //输出链表一
  43.         struct Node *  pmove=init_link(head);
  44.         while (pmove)
  45.         {
  46.                 printf("%4d  ",pmove->data);
  47.                 if(pmove->next==NULL) break;     
  48.                 pmove=pmove->next;
  49.         }
  50.         printf("\n");
  51.         //输出链表二
  52.         struct Node * head2=NULL;
  53.         head2=(struct Node*)malloc(sizeof(struct Node));///////////////////////////////
  54.         head2->data=0;
  55.         head2->next=NULL;
  56.         struct Node *  pmove2=init_link2(head2);
  57.         while (pmove2)
  58.         {
  59.              printf("%4d  ",pmove2->data);
  60.              if(pmove2->next==NULL) break;     
  61.              pmove2=pmove2->next;
  62.         }
  63.         printf("\n");
  64.         return 0;
  65. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-10 13:01:37 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=NULL;
  12.     for(int x=10;x<20;x++)
  13.     {
  14.         struct Node * node1=NULL;
  15.         node1=(struct Node*)malloc(sizeof(struct Node));/////////////////////////////////////////////是结构体的大小,不是结构体指针的大小
  16.         node1->data=x;
  17.         node1->next=head;
  18.         head=node1;
  19.     }
  20.     return head;
  21. }

  22. //带头节点的 头插法
  23. struct Node * init_link2(struct Node * s)
  24. {
  25.     struct Node * head=NULL;
  26.     for(int x=100;x<110;x++)
  27.     {
  28.         struct Node * node1=NULL;
  29.         node1=(struct Node*)malloc(sizeof(struct Node));////////////////////////////////////
  30.         node1->data=x;
  31.         node1->next=head;
  32.         head=node1;
  33.     }
  34.     return head;
  35. }


  36. int main()
  37. {
  38.         struct Node * head=NULL;
  39.         head=(struct Node*)malloc(sizeof(struct Node));///////////////////////////
  40.         head->data=0;
  41.         head->next=NULL;
  42.         //输出链表一
  43.         struct Node *  pmove=init_link(head);
  44.         while (pmove)
  45.         {
  46.                 printf("%4d  ",pmove->data);
  47.                 if(pmove->next==NULL) break;     
  48.                 pmove=pmove->next;
  49.         }
  50.         printf("\n");
  51.         //输出链表二
  52.         struct Node * head2=NULL;
  53.         head2=(struct Node*)malloc(sizeof(struct Node));///////////////////////////////
  54.         head2->data=0;
  55.         head2->next=NULL;
  56.         struct Node *  pmove2=init_link2(head2);
  57.         while (pmove2)
  58.         {
  59.              printf("%4d  ",pmove2->data);
  60.              if(pmove2->next==NULL) break;     
  61.              pmove2=pmove2->next;
  62.         }
  63.         printf("\n");
  64.         return 0;
  65. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-10 13:09:22 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  7. //带头节点的 头插法
  8. //struct Node *init_link(struct Node *s) {
  9. struct Node *init_link(void) {
  10.     struct Node *head = NULL;
  11.     for(int x = 10; x < 20; x++) {
  12.         struct Node *node1 = NULL;
  13.         //node1 = (struct Node *)malloc(sizeof(struct Node *));
  14.         //node1 = malloc(sizeof(struct Node));
  15.         node1 = malloc(sizeof(*node1));
  16.         node1->data = x;
  17.         node1->next = head;
  18.         head = node1;
  19.     }
  20.     return head;
  21. }

  22. //带头节点的 头插法
  23. //struct Node *init_link2(struct Node *s) {
  24. struct Node *init_link2(void) {
  25.     struct Node *head = NULL;
  26.     for(int x = 100; x < 110; x++) {
  27.         struct Node *node1 = NULL;
  28.         //node1 = (struct Node *)malloc(sizeof(struct Node *));
  29.         node1 = malloc(sizeof(*node1));
  30.         node1->data = x;
  31.         node1->next = head;
  32.         head = node1;
  33.     }
  34.     return head;
  35. }

  36. void list_deinit(struct Node *l) {
  37.     if(!l) return;
  38.     list_deinit(l->next);
  39.     free(l);
  40. }

  41. int main() {
  42.     /*
  43.     struct Node *head = NULL;
  44.     //head = (struct Node *)malloc(sizeof(struct Node *));
  45.     head = malloc(sizeof(*head));
  46.     head->data = 0;
  47.     head->next = NULL;
  48.     */
  49.     //输出链表一
  50.     //struct Node *pmove = init_link(head);
  51.     struct Node *pmove = init_link();
  52.     struct Node *bak = pmove;
  53.     while(pmove) {
  54.         printf("%4d  ", pmove->data);
  55.         //if(pmove->next == NULL)
  56.             //break;
  57.         pmove = pmove->next;
  58.     }
  59.     pmove = bak;
  60.     list_deinit(pmove);
  61.     printf("\n");
  62.     //输出链表二

  63.     /*
  64.     struct Node *head2 = NULL;
  65.     //head2 = (struct Node *)malloc(sizeof(struct Node *));
  66.     head2 = malloc(sizeof(*head2));
  67.     head2->data = 0;
  68.     head2->next = NULL;
  69.     */
  70.     //struct Node *pmove2 = init_link2(head2);
  71.     struct Node *pmove2 = init_link2();
  72.     bak = pmove2;
  73.     while(pmove2) {
  74.         printf("%4d  ", pmove2->data);
  75.         if(pmove2->next == NULL)
  76.             break;
  77.         pmove2 = pmove2->next;
  78.     }
  79.     pmove2 = bak;
  80.     list_deinit(pmove2);
  81.     printf("\n");
  82.     return 0;
  83. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-10 13:13:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-10 13:26:21 From FishC Mobile | 显示全部楼层
人造人 发表于 2022-5-10 13:09

void list_deinit(struct Node *l) {
    if(!l) return;
    list_deinit(l->next);
    free(l);
}
??????释放所有指针???
我还以为链表里的所有指针都不能释放
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-10 13:27:11 | 显示全部楼层
wp231957 发表于 2022-5-10 13:26
void list_deinit(struct Node *l) {
    if(!l) return;
    list_deinit(l->next);

嗯,申请了内存得释放
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 12:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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