重名大师 2023-02-18 22:42 采纳率: 96.6%
浏览 25
已结题

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

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


#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->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;
    }


  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-02-19 00:09
    关注

    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;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 已采纳回答 3月16日
  • 创建了问题 2月18日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。