#include <stdlib.h>
#include <stdio.h>
typedef struct student
{
int num;
char name[20];
struct student *next;
}Code;
Code *Creat(int length);
Code *Delete(Code *ahead,Code *bhead);
void Print(Code *head);
void main()
{
Code *ahead,*bhead;
int a_length,b_length;
printf("input length of list_a:\n");
scanf("%d",&a_length);
ahead=Creat(a_length);
Print(ahead);
printf("input length of list_b:\n");
scanf("%d",&b_length);
bhead=Creat(b_length);
Print(bhead);
ahead=Delete(ahead,bhead);
Print(ahead);
}
//建立链表函数
Code *Creat(int length)
{
Code *p1,*p2,*head;
int i = 1;
printf("Now,input the students' information:\n");
head = ( Code * )malloc( sizeof( Code ) );
head->next = NULL;
p1 = p2 = head;
while( i <= length ){
p1=(Code *)malloc(sizeof(Code));
scanf("%d %s",&p1->num,p1->name);
p2->next = p1;
p2 = p1;
i++;
}
p1->next = NULL;
return head;
}
//输出链表函数
void Print(Code *head)
{
Code *p;
p=head->next;
if( p !=NULL)
do
{
printf("number:%d
name:%s\n",p->num,p->name);
p=p->next;
}while(p!=NULL);
}
//删除相同学号函数
Code *Delete(Code *ahead,Code *bhead)
{
Code *pa1,*pa2,*pb1;
pb1=bhead->next;
while(pb1!=NULL)
//让b表中的每个结点和a表比较
{
pa1=ahead->next;
pa2 = ahead;
while((pa1!=NULL)&&(pb1->num !=pa1->num))
{
pa1=pa1->next;
pa2 = pa2->next;
}
printf("运行\n");
if(
pa1 != NULL)
//两链表中学号相同
{
pa2->next = pa1->next;
free( pa1 );
pa1 = pa1->next;
}
pb1 = pb1->next;
}
return ahead;
}
//我刚刚在你的程序上改了下,用的带头结点的链表,运行了下可以,不知道结果符不符合要求,你试试