鱼C论坛

 找回密码
 立即注册
查看: 1393|回复: 6

[已解决]链表释放问题

[复制链接]
发表于 2021-2-15 17:22:38 | 显示全部楼层 |阅读模式

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

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

x
运行时突然出现debug,请问代码这样打哪里有问题?

void shifang(struct nb *head)
{
        struct nb *p1,*p2;
        p1=head;
        while(p1)
        {
                p2=p1->next;
                free(p1);
                p1=p2;
        }
        head->next=NULL;
        printf("OK!");
}
最佳答案
2021-2-15 19:54:07
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>


  4. #define LEN sizeof(struct nb)


  5. struct nb
  6. {
  7.         long xh;
  8.         float cj;
  9.         struct nb *next;
  10. };


  11. void sc(struct nb *head)
  12. {
  13.         struct nb *b;
  14.         b=head;
  15.         if(head)
  16.         {
  17.             do
  18.             {
  19.                 printf("%d   %f\n",b->xh,b->cj);
  20.                 b=b->next;
  21.             }while(b);
  22.         }
  23. }


  24. struct nb *xin()
  25. {
  26.         int n=0;
  27.     struct nb *head,*p1,*p2;
  28.         p1=p2=(struct nb *)malloc(LEN);
  29.         head=NULL;
  30.         scanf("%ld,%f",&p1->xh,&p1->cj);
  31.         while(p1->xh)
  32.         {
  33.                 n=n+1;
  34.                 if(n==1)
  35.                     head=p1;
  36.                 else
  37.                     p2->next=p1;
  38.                 p2=p1;
  39.                 p1=(struct nb *)malloc(LEN);
  40.                 scanf("%ld,%f",&p1->xh,&p1->cj);
  41.         }
  42.         p2->next=NULL;
  43.         return head;
  44. }


  45. struct nb *xj(struct nb *head,struct nb *stud)
  46. {
  47.         struct nb *p0,*p1,*p2;
  48.         p1=head,p0=stud;
  49.         if(p1==NULL)
  50.         {
  51.                 head=p0;
  52.                 p0->next=NULL;
  53.         }
  54.         else
  55.         {
  56.                 while((p0->xh>p1->xh)&&(p1->next!=NULL))
  57.                 {
  58.                         p2=p1;
  59.                         p1=p1->next;
  60.                 }
  61.                 if(p0->xh<=p1->xh)
  62.                 {
  63.                         if(p1==head)
  64.                                 head=p0;
  65.                         else
  66.                                 p2->next=p0;
  67.                         p0->next=p1;
  68.                 }
  69.                 else
  70.                 {
  71.                         p1->next=p0;
  72.                         p0->next=NULL;
  73.                 }
  74.         }
  75.         return head;
  76. }


  77. struct nb *del(struct nb *head,int num)
  78. {
  79.         struct nb *p1,*p2;
  80.         if(head==NULL)
  81.         {
  82.                 printf("null");
  83.                 goto END;
  84.         }
  85.         p1=head;
  86.         while(num!=p1->xh&&p1->next!=NULL)
  87.         {
  88.                 p2=p1;
  89.                 p1=p1->next;
  90.         }
  91.         if(p1->xh==num)
  92.         {
  93.                 if(p1==head)
  94.                         head=p1->next;
  95.                 else
  96.                         p2->next=p1->next;
  97.         }
  98.         else
  99.                 printf("not found\n");

  100. END:
  101.         return head;
  102. }


  103. void search(struct nb *head,long number)
  104. {
  105.         struct nb *p1,*p2;
  106.         p1=head;
  107.         if(head==NULL)
  108.         {
  109.                 printf("null\n");
  110.                 goto ENG;
  111.         }
  112.         else
  113.         {
  114.                 while((p1->xh!=number)&&(p1->next!=NULL))
  115.                 {
  116.                         p2=p1;
  117.                         p1=p1->next;
  118.                 }
  119.                 if(p1->xh==number)
  120.                         printf("%d   %f\n",p1->xh,p1->cj);
  121.                 else
  122.                         printf("not found\n");
  123.         }
  124. ENG:
  125.         printf("ok!\n");
  126. }


  127. void shifang(struct nb *head)
  128. {
  129.         struct nb *p1,*p2;
  130.         p1=head;
  131.         while(p1)
  132.         {
  133.                 p2=p1->next;
  134.                 free(p1);                        
  135.                 p1=NULL;
  136.                 p1=p2;
  137.         }
  138.         printf("OK!");
  139. }


  140. void main()
  141. {
  142.         int e,f;
  143.         struct nb *a,*b,*c,stu;
  144.         a=xin();
  145.         sc(a);
  146.         printf("\n");


  147.         scanf("%d",&e);
  148.         b=del(a,e);
  149.         sc(b);
  150.         printf("\n");


  151.         scanf("%d,%f",&stu.xh,&stu.cj);
  152.         c=xj(b,&stu);
  153.         sc(c);
  154.         printf("\n");


  155.         scanf("%d",&f);
  156.         search(c,f);


  157.         shifang(c);


  158. }
复制代码

没发现有错误,只是有些输出因为没加\n,导致前一个数与后一个数连在一起了,我已经加了,还有的是,你的程序最大的问题是,运行到输入的时候竟然没任何提示要输入什么,至少输出点什么提示一下(这个请自己加),还有,你的scanf输入格式要对齐:例如这个scanf("%d,%f",&p1->xh,&p1->cj);  数之间的,千万别忘记写上去,要的是英文格式下输入的逗号,不是中文格式的。
还有,下次发代码请带上注释,都不知道你那函数干嘛用的。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-15 17:49:58 From FishC Mobile | 显示全部楼层
head->next=NULL;这行去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-15 18:02:56 | 显示全部楼层
本帖最后由 一叶枫残 于 2021-2-15 18:06 编辑

这个是因为你把head的值给了p1,然后释放了p1的内存,相当于释放了head的内存,这样head->next已经没有数据了,于是不能赋值给NULL,其实应该这样做;
  1. void shifang(struct nb *head)
  2. {
  3.         struct nb *p1,*p2;
  4.         p1=head;
  5.         while(p1)
  6.         {
  7.                 p2=p1->next;
  8.                 free(p1);                       
  9.                 p1 = NULL;                //此时p1还保存着地址值,直接赋值NULL使这个值不为野指针
  10.                 p1=p2;                        //给p1下一个地址值,不会对上一步的值为NULL造成影响
  11.         }
  12.         printf("OK!");
  13. }
复制代码

这样能保证每个指针都不会成为野指针;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-15 18:12:19 | 显示全部楼层
一叶枫残 发表于 2021-2-15 18:02
这个是因为你把head的值给了p1,然后释放了p1的内存,相当于释放了head的内存,这样head->next已经没有数据 ...

还是运行时提示debug,麻烦你再帮我看看,下面是全部代码




#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>


#define LEN sizeof(struct nb)


struct nb
{
        long xh;
        float cj;
        struct nb *next;
};


void sc(struct nb *head)
{
        struct nb *b;
        b=head;
        if(head)
        {
                do
                {
                        printf("%d   %f",b->xh,b->cj);
                        b=b->next;
                }while(b);
        }
}


struct nb *xin()
{
        int n=0;
        struct nb *head,*p1,*p2;
        p1=p2=(struct nb *)malloc(LEN);
        head=NULL;
        scanf("%d,%f",&p1->xh,&p1->cj);
        while(p1->xh)
        {
                n=n+1;
                if(n==1)
                        head=p1;
                else
                        p2->next=p1;
                p2=p1;
                p1=(struct nb *)malloc(LEN);
                scanf("%d,%f",&p1->xh,&p1->cj);
        }
        p2->next=NULL;
        return head;
}


struct nb *xj(struct nb *head,struct nb *stud)
{
        struct nb *p0,*p1,*p2;
        p1=head,p0=stud;
        if(p1==NULL)
        {
                head=p0;
                p0->next=NULL;
        }
        else
        {
                while((p0->xh>p1->xh)&&(p1->next!=NULL))
                {
                        p2=p1;
                        p1=p1->next;
                }
                if(p0->xh<=p1->xh)
                {
                        if(p1==head)
                                head=p0;
                        else
                                p2->next=p0;
                        p0->next=p1;
                }
                else
                {
                        p1->next=p0;
                        p0->next=NULL;
                }
        }
        return head;
}


struct nb *del(struct nb *head,long num)
{
        struct nb *p1,*p2;
        if(head==NULL)
        {
                printf("null");
                goto END;
        }
        p1=head;
        while(num!=p1->xh&&p1->next!=NULL)
        {
                p2=p1;
                p1=p1->next;
        }
        if(p1->xh==num)
        {
                if(p1==head)
                        head=p1->next;
                else
                        p2->next=p1->next;
        }
        else
                printf("not found");

END:
        return head;
}


void search(struct nb *head,long number)
{
        struct nb *p1,*p2;
        p1=head;
        if(head==NULL)
        {
                printf("null\n");
                goto ENG;
        }
        else
        {
                while((p1->xh!=number)&&(p1->next!=NULL))
                {
                        p2=p1;
                        p1=p1->next;
                }
                if(p1->xh==number)
                        printf("%d   %f",p1->xh,p1->cj);
                else
                        printf("not found\n");
        }
ENG:
        printf("ok!\n");
}


void shifang(struct nb *head)
{
        struct nb *p1,*p2;
        p1=head;
        while(p1)
        {
                p2=p1->next;
                free(p1);                        
                p1=NULL;
                p1=p2;
        }
        printf("OK!");
}


void main()
{
        int e,f;
        struct nb *a,*b,*c,stu;
        a=xin();
        sc(a);
        printf("\n");


        scanf("%d",&e);
        b=del(a,e);
        sc(b);
        printf("\n");


        scanf("%d,%f",&stu.xh,&stu.cj);
        c=xj(b,&stu);
        sc(c);
        printf("\n");


        scanf("%d",&f);
        search(c,f);


        shifang(c);


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

使用道具 举报

发表于 2021-2-15 19:54:07 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>


  4. #define LEN sizeof(struct nb)


  5. struct nb
  6. {
  7.         long xh;
  8.         float cj;
  9.         struct nb *next;
  10. };


  11. void sc(struct nb *head)
  12. {
  13.         struct nb *b;
  14.         b=head;
  15.         if(head)
  16.         {
  17.             do
  18.             {
  19.                 printf("%d   %f\n",b->xh,b->cj);
  20.                 b=b->next;
  21.             }while(b);
  22.         }
  23. }


  24. struct nb *xin()
  25. {
  26.         int n=0;
  27.     struct nb *head,*p1,*p2;
  28.         p1=p2=(struct nb *)malloc(LEN);
  29.         head=NULL;
  30.         scanf("%ld,%f",&p1->xh,&p1->cj);
  31.         while(p1->xh)
  32.         {
  33.                 n=n+1;
  34.                 if(n==1)
  35.                     head=p1;
  36.                 else
  37.                     p2->next=p1;
  38.                 p2=p1;
  39.                 p1=(struct nb *)malloc(LEN);
  40.                 scanf("%ld,%f",&p1->xh,&p1->cj);
  41.         }
  42.         p2->next=NULL;
  43.         return head;
  44. }


  45. struct nb *xj(struct nb *head,struct nb *stud)
  46. {
  47.         struct nb *p0,*p1,*p2;
  48.         p1=head,p0=stud;
  49.         if(p1==NULL)
  50.         {
  51.                 head=p0;
  52.                 p0->next=NULL;
  53.         }
  54.         else
  55.         {
  56.                 while((p0->xh>p1->xh)&&(p1->next!=NULL))
  57.                 {
  58.                         p2=p1;
  59.                         p1=p1->next;
  60.                 }
  61.                 if(p0->xh<=p1->xh)
  62.                 {
  63.                         if(p1==head)
  64.                                 head=p0;
  65.                         else
  66.                                 p2->next=p0;
  67.                         p0->next=p1;
  68.                 }
  69.                 else
  70.                 {
  71.                         p1->next=p0;
  72.                         p0->next=NULL;
  73.                 }
  74.         }
  75.         return head;
  76. }


  77. struct nb *del(struct nb *head,int num)
  78. {
  79.         struct nb *p1,*p2;
  80.         if(head==NULL)
  81.         {
  82.                 printf("null");
  83.                 goto END;
  84.         }
  85.         p1=head;
  86.         while(num!=p1->xh&&p1->next!=NULL)
  87.         {
  88.                 p2=p1;
  89.                 p1=p1->next;
  90.         }
  91.         if(p1->xh==num)
  92.         {
  93.                 if(p1==head)
  94.                         head=p1->next;
  95.                 else
  96.                         p2->next=p1->next;
  97.         }
  98.         else
  99.                 printf("not found\n");

  100. END:
  101.         return head;
  102. }


  103. void search(struct nb *head,long number)
  104. {
  105.         struct nb *p1,*p2;
  106.         p1=head;
  107.         if(head==NULL)
  108.         {
  109.                 printf("null\n");
  110.                 goto ENG;
  111.         }
  112.         else
  113.         {
  114.                 while((p1->xh!=number)&&(p1->next!=NULL))
  115.                 {
  116.                         p2=p1;
  117.                         p1=p1->next;
  118.                 }
  119.                 if(p1->xh==number)
  120.                         printf("%d   %f\n",p1->xh,p1->cj);
  121.                 else
  122.                         printf("not found\n");
  123.         }
  124. ENG:
  125.         printf("ok!\n");
  126. }


  127. void shifang(struct nb *head)
  128. {
  129.         struct nb *p1,*p2;
  130.         p1=head;
  131.         while(p1)
  132.         {
  133.                 p2=p1->next;
  134.                 free(p1);                        
  135.                 p1=NULL;
  136.                 p1=p2;
  137.         }
  138.         printf("OK!");
  139. }


  140. void main()
  141. {
  142.         int e,f;
  143.         struct nb *a,*b,*c,stu;
  144.         a=xin();
  145.         sc(a);
  146.         printf("\n");


  147.         scanf("%d",&e);
  148.         b=del(a,e);
  149.         sc(b);
  150.         printf("\n");


  151.         scanf("%d,%f",&stu.xh,&stu.cj);
  152.         c=xj(b,&stu);
  153.         sc(c);
  154.         printf("\n");


  155.         scanf("%d",&f);
  156.         search(c,f);


  157.         shifang(c);


  158. }
复制代码

没发现有错误,只是有些输出因为没加\n,导致前一个数与后一个数连在一起了,我已经加了,还有的是,你的程序最大的问题是,运行到输入的时候竟然没任何提示要输入什么,至少输出点什么提示一下(这个请自己加),还有,你的scanf输入格式要对齐:例如这个scanf("%d,%f",&p1->xh,&p1->cj);  数之间的,千万别忘记写上去,要的是英文格式下输入的逗号,不是中文格式的。
还有,下次发代码请带上注释,都不知道你那函数干嘛用的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-15 20:07:20 From FishC Mobile | 显示全部楼层
一叶枫残 发表于 2021-2-15 19:54
没发现有错误,只是有些输出因为没加\n,导致前一个数与后一个数连在一起了,我已经加了,还有的是,你的 ...

请问你用的编译器是哪一个?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-15 20:14:56 | 显示全部楼层
wdwhszw 发表于 2021-2-15 20:07
请问你用的编译器是哪一个?

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-1 19:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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