C语言 删除一个链表中数据值相等的结点,只保留最前面一个 20

#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedefstructs{inti;structs*next... #include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct s
{ int i;
struct s *next;
};
int main()
{
struct s *head,*p,*q,*t;
head=(struct s *)malloc(sizeof(struct s));
p=head;
scanf("%d",&p->i);
while(p->i!=0)
{
q=(struct s *)malloc(sizeof(struct s));
p->next=q;
scanf("%d",&q->i);
p=q;
}
p->next=0;
p=head;
while(p->next!=0)
{ q=p->next;
while(p->i!=q->i&&q->next!=0)
{t=q;
q=q->next;}
if(p->i==q->i)
t->next=q->next;
else if(q->next)
p=p->next;
}

q=head;
while(q->i!=0)
{
printf("%4d",q->i);
q=q->next;
}
printf("\n");
return 0;
}
哪里错了???
貌似死循环了
展开
 我来答
匿名用户
2014-12-24
展开全部
//while里错了
 while(p->next!=0)
 {  
  q=p->next;
  while(p->i!=q->i&&q->next!=0)
  {
   t=q;
   q=q->next;
   if(p->i==q->i)
   t->next=q->next;
  }
  //else if(q->next)
   p=p->next;
 } 
//还有最后那个while应该改成q->next != 0
追问
还是不能运行
追答
最后的注释提到的你改了吗?
什么问题现在
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式