这个删除重复节点的链表为何一直错误

用链表删除重复元素,以0为结尾,只会C语言,不会c++,求指点


#include
#include
struct cell{
int x;
struct cell*next;};
struct cell*build (void)
{
    struct cell*head,*p,*tmp;
    head=p=tmp=NULL;
    int n;
    scanf("%d",&n);
    if(n==0){return head;}
    p=(struct cell*)malloc(sizeof(struct cell));
    p->x=n;
    p->next=NULL;
    head=p;
    scanf("%d",&n);
    while(n!=0)
    {
        tmp=(struct cell*)malloc(sizeof(struct cell));
        tmp->x=n;
        tmp->next=NULL;
        p->next=tmp;
        p=p->next;
        scanf("%d",&n);
    }
    return head;
};
void cut(struct cell*head)
{
   struct cell*p,*q,*p0,*q0;
   p=head;
   while(p->next!=NULL)
   {q0=p;
       q=p->next;
       while(q!=NULL)
       {
           if(p->x==q->x){if(q->next!=NULL){q0=q;q=q->next;free(q0);}else{q0->next=NULL;free(q);q=q0->next;}}
           else q=q->next;
       }
       p=p->next;
   }

}
void rint(struct cell*head)
{
    if(head==NULL){printf("NULL");return ;}
    struct cell*p,*p0;
    p=head;
    while(p!=NULL)
    {
        printf("%d",p->x);
        p=p->next;
    }

}
void release(struct cell*head)
{
    struct cell*p,*p0;
    p=head;
    while(p!=0)
        {
            p0=p;p=p->next;free(p0);
        }
    }
    int main()
    {
        struct cell*head;
        head=build();

        cut(head);
        rint(head);
        release(head);
        return 0;
    }


void cut(struct cell*head) 函数修改如下,改动处见注释,供参考:

#include<stdio.h>
#include<malloc.h>
struct cell{
    int x;
    struct cell*next;
};
struct cell*build (void)
{
    struct cell*head,*p,*tmp;
    head=p=tmp=NULL;
    int n;
    scanf("%d",&n);
    if(n==0){return head;}
    p=(struct cell*)malloc(sizeof(struct cell));
    p->x=n;
    p->next=NULL;
    head=p;
    scanf("%d",&n);
    while(n!=0)
    {
        tmp=(struct cell*)malloc(sizeof(struct cell));
        tmp->x=n;
        tmp->next=NULL;
        p->next=tmp;
        p=p->next;
        scanf("%d",&n);
    }
    return head;
};
void cut(struct cell*head)
{
   struct cell*p,*q,*p0,*q0;
   p=head;
   while(p != NULL) //(p->next!=NULL) 修改
   {
       q0=p;
       q=p->next;
       while(q!=NULL) 
       {
           if(p->x==q->x){
               if(q->next!=NULL) //{  修改
                         //q0=q;      修改
                         //q=q->next; 修改
                   q0->next=q->next;
                         //free(q0);  修改
                         //q0=q;      修改
               else      // } {       修改
                   q0->next=NULL;
               free(q);             //修改
               q=q0->next;          //修改
                         //}          修改
           }
           else{         //{          修改
               q0=q;                //修改
               q=q->next;
           }
       }
       p=p->next;
   }
}
void rint(struct cell*head)
{
    if(head==NULL){printf("NULL");return ;}
    struct cell*p,*p0;
    p=head;
    while(p!=NULL)
    {
        printf("%d",p->x);
        p=p->next;
    }
 
}
void release(struct cell*head)
{
    struct cell*p,*p0;
    p=head;
    while(p!=0)
    {
        p0=p;p=p->next;free(p0);
    }
}
int main()
{
    struct cell*head;
    head=build();

    cut(head);
    rint(head);
    release(head);
    return 0;
}


啥错误现象啊?
11行输入了n,17行不需要再输入一次了

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^