有两个链表,节点中包含学号和姓名。从a链表中删去与b链表中有相同学号的节点。(最好能帮我指出错误,谢谢了)
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(student)
int n;
struct student
{
long num;
char name[20];
struct student *next;
};
struct student* creat(void)
{ n=0;
struct student *p1,*p2,*head;
p1=p2=(struct student*)malloc(LEN);
head=NULL;
printf("please input the number of this student:");
scanf_s("%ld",&p1->num);
getchar();
printf("please input the name of this student:");
gets(p1->name);
if(p1->num!=0)
{
for(;p1->num!=0;)
{
n=n+1;
if(n==1){head =p1;}
else{p2->next=p1;}
p2=p1;
p1=(struct student*)malloc(LEN);
printf("please input the number of this student:");
scanf_s("%ld",&p1->num);
getchar();
if(p1->num==0)break;
printf("please input the name of this student:");
gets(p1->name);
}
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
if(p!=NULL)
{
do
{
printf("学号:%ld,姓名:%s\n",p->num,p->name);
p=p->next;
}while(p!=NULL);
}
}
struct student *del(struct student *ahead ,struct student *bhead)
{
struct student *a1,*a2,*b1;
if(ahead==NULL||bhead==NULL)
{printf("\nlist full\n");return ahead;}
a1=ahead;
b1=bhead;
while(b1->next!=NULL&&a1->next!=NULL)
{
while(b1->num!=a1->num&&a1->next!=NULL)
{a2=a1;a1=a1->next;}
if(b1->num==a1->num)
{
if(a1==ahead)
ahead=a1->next;
else
a2->next=a1->next;
printf("delete:%d\n",b1->num);
n=n-1;
}
else printf("%ld not been found!",b1->num);
b1=b1->next;
}
return ahead;
}
void main()
{
struct student * listA,* listB;
printf("please input listA:");
listA=creat();
printf("please input listB:");
listB=creat();
print(listA);
print(listB);
//del(listA,listB);
//printf("\n\n");
//print(listA);
//print(listB);
}
本数据来源于百度地图,最终结果以百度地图最新数据为准。