
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;
}
哪里错了???
貌似死循环了 展开
#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;
}
哪里错了???
貌似死循环了 展开
1个回答
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
追问
还是不能运行
追答
最后的注释提到的你改了吗?
什么问题现在
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询