69,382
社区成员
发帖
与我相关
我的任务
分享
#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);
}