wdwhszw 发表于 2021-2-15 17:22:38

链表释放问题

运行时突然出现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!");
}

xieglt 发表于 2021-2-15 17:49:58

head->next=NULL;这行去掉

一叶枫残 发表于 2021-2-15 18:02:56

本帖最后由 一叶枫残 于 2021-2-15 18:06 编辑

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

wdwhszw 发表于 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);


}

一叶枫残 发表于 2021-2-15 19:54:07

#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\n",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("%ld,%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("%ld,%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,int 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\n");

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\n",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);


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

wdwhszw 发表于 2021-2-15 20:07:20

一叶枫残 发表于 2021-2-15 19:54
没发现有错误,只是有些输出因为没加\n,导致前一个数与后一个数连在一起了,我已经加了,还有的是,你的 ...

请问你用的编译器是哪一个?

一叶枫残 发表于 2021-2-15 20:14:56

wdwhszw 发表于 2021-2-15 20:07
请问你用的编译器是哪一个?

dev
页: [1]
查看完整版本: 链表释放问题