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

wenqiguo wenqiguo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先上源码:
#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);
}
...全文
评论 18
收藏 已收藏
分享
返回顶部
wenqiguo
C语言 70,040 成员 243,237 内容