关于链表的问题,高手请指教!!!!

wenqiguo 2010-01-04 07:37:42
先上源码:
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct people)
struct people
{int num;
char name[20];
struct people *next;
};
int n;
struct people *creat(void)//建立链表
{struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people*)malloc(LEN);//开辟一个新单元
scanf("%d%s",&p1->num,&p1->name);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%d%s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}
struct people *del(struct people *head,int num)//删除结点
{struct people *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{ p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
n=n-1;
}
end:
return(head);}
void main()
{
struct people *head1,*head2,*p;
int num;
printf("输入链表a:\n");
head1=creat();
printf("输入链表b:\n");
head2=creat();
loop:for(;head1->num==head2->num;head2=head2->next)
{
num=head1->num;
head1=del(head1,num);
}
if(head1->num!=head2->num)
{
head1=head1->next;
goto loop;
}
printf("处理后的a链表为:\n");//编译能通过,运行到这时就出现程序终止???
p=head1;
if(head1!=NULL)
do
{
printf("%d %s\n",p->num,p->name);
p=p->next;
}
while(p!=NULL);
}
...全文
202 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
renzhewh 2010-01-05
  • 打赏
  • 举报
回复
loop:for(;head1->num==head2->num,head2!=NULL;head2=head2->next)//这里有错!!!!!!,我没运行程序,感觉判断条件中的逗号运算符有错,应改成&&。

另外 如楼上scanf("%d%s",&p1->num,&p1->name);
&p1->name 应把 & 去掉
wenqiguo 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 cangyingzhijia 的回复:]
ls都说了
jf
[/Quote]不对
苍蝇①号 2010-01-05
  • 打赏
  • 举报
回复
ls都说了
jf
wenqiguo 2010-01-05
  • 打赏
  • 举报
回复
就循环那有问题!!
这是我自己改正后的源码:(但还是有错,请高手指教)
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct people)
struct people
{int num;
char name[20];
struct people *next;
};
int n;
struct people *creat(void)//建立链表
{struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people*)malloc(LEN);//开辟一个新单元
scanf("%d%s",&p1->num,&p1->name);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%d%s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}
struct people *del(struct people *head,int num)//删除结点
{struct people *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{ p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
n=n-1;
}
end:
return(head);}
void main()
{
struct people *head1,*head2,*head3,*p;
int num;
printf("输入链表a:\n");
head1=creat();
printf("输入链表b:\n");
head2=creat();
loop:for(;head1->num==head2->num,head2!=NULL;head2=head2->next)//这里有错!!!!!!,请高手只出
{
num=head1->num;
head3=del(head1,num);
}
if(head1->num!=head2->num&&head1!=NULL)
{
head1=head1->next;
goto loop;
}
printf("处理后的a链表为:\n");
p=head3;
if(head3!=NULL)
do
{
printf("%d %s\n",p->num,p->name);
p=p->next;
}
while(p!=NULL);
}
woaipanit 2010-01-05
  • 打赏
  • 举报
回复
struct people *del(struct people *head,int num)//删除结点
{struct people *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");return head;}
p1=p2=head;
while(p2)
{
if(p2->num==num){if(p2->next!=NULL){p1->next=p2->next};
else
p1->next=NULL;
return head;}
else {p2=p1->next;}
}
}
woodwoods 2010-01-04
  • 打赏
  • 举报
回复
最好还是不用goto语句
东大坡居士 2010-01-04
  • 打赏
  • 举报
回复
不好意思,看错了~~~抱歉~~~
show_bill 2010-01-04
  • 打赏
  • 举报
回复
好多错误啊,而且程序结构不好,goto没有必要, 对链表操作的时候条件指针是否为空都没有判断,
在建立链表时head并没有赋值
jernymy 2010-01-04
  • 打赏
  • 举报
回复
加了个保护,不过输入数据是可以的,只是运行不知道楼主的意思。


#include <stdio.h>
#include <malloc.h>

#define LEN sizeof(struct people)

struct people
{
int num;
char name[20];
struct people *next;
};

int n;
struct people *creat(void)//建立链表
{
struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people*)malloc(LEN);//开辟一个新单元
scanf("%d%s",&p1->num,&p1->name);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%d%s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}

struct people *del(struct people *head,int num)//删除结点
{
struct people *p1,*p2;

if(head==NULL)
{
printf("\nlist null!\n");
goto end;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
}
end:
return(head);
}

void main()
{
struct people *head1,*head2,*p;
int num;

printf("输入链表a:\n");
head1=creat();
printf("输入链表b:\n");
head2=creat();
loop:
if (NULL == head1)
{
printf("head1 check finished\n");
return ;
}
for (;head1->num==head2->num;head2=head2->next)
{
num=head1->num;
head1=del(head1,num);
}
if( head1->num!=head2->num)
{
head1=head1->next;
goto loop;
}
printf("处理后的a链表为:\n");//编译能通过,运行到这时就出现程序终止???
p=head1;
if(head1!=NULL)
do
{
printf("%d %s\n",p->num,p->name);
p=p->next;
} while (p!=NULL);
}

东大坡居士 2010-01-04
  • 打赏
  • 举报
回复
可以在这个while之前,在p1 malloc之后,将p1赋给head,另外,
scanf("%d%s",&p1->num,&p1->name); 这个也应该是错了,要改成
scanf("%d%s",&p1->num,p1->name);
东大坡居士 2010-01-04
  • 打赏
  • 举报
回复
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%d%s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}

//这块你新建的时候错了,每次申请完一个结点,都将head=p1,最后head都指向最后节点了,当然就什么也不能输出了
hrlhrl0 2010-01-04
  • 打赏
  • 举报
回复
head搞错了吧
wenqiguo 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 swl82560397pq 的回复:]
scanf("%d%s",&p1->num,&p1->name);改成p1->name.
[/Quote]改了后也不对,数据都输不进了
wenqiguo 2010-01-04
  • 打赏
  • 举报
回复
改了后也不对啊!数据都输不进!!
swl82560397pq 2010-01-04
  • 打赏
  • 举报
回复
另外最好别用goto语句把!
swl82560397pq 2010-01-04
  • 打赏
  • 举报
回复
scanf("%d%s",&p1->num,&p1->name);改成p1->name.
wenqiguo 2010-01-04
  • 打赏
  • 举报
回复
没人知道吗
guojianxun17953 2010-01-04
  • 打赏
  • 举报
回复
友情帮顶

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧