给你个参考例子,自己去改吧。
#include
#define Len sizeof(struct student)
struct student
{
long num;
float score;
struct student * next;
};
int n;
int main()
{
struct student *a=0,*b=0;
struct student *p1,*p2,*p3;
n=0;
while(n<5)/*建立链表a*/
{
n+=1;
p1=(struct student *)malloc(Len);
if(n==1)
{
a=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
p1->num=n;
p1->score=80+n;
}
p1->next=0;
n=0;
while(n<3)/*建立b链表*/
{
n+=1;
p1=(struct student *)malloc(Len);
if(n==1)
{
b=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
p1->num=n+1;
p1->score=80+n+1;
}
p1->next=0;
p1=a;
while(p1->next!=0)/*显示a链表*/
{
printf("%d %0.2f ",p1->num,p1->score);
p1=p1->next;
}
printf("\n");
p1=b;
while(p1->next!=0)/*显示b链表*/
{
printf("%d %0.2f ",p1->num,p1->score);
p1=p1->next;
}
printf("\n\n");
p3=b;
while (p3->next!=0)/*查找删除a中的b*/
{
p1=a;
while((p3->num!=p1->num)&&(p1->next!=0))
{p2=p1;p1=p1->next;}
if(p3->num==p1->num)/*找到一个*/
{
if(p1==a)
a=p1->next;
else
p2->next=p1->next;
printf("delete:%ld\n",p3->num);
}
else printf("%ld not been found!\n",p3->num);
p3=p3->next;
}
printf("\n");
p1=a;
while(p1->next!=0)/*显示删除b后的a链表*/
{
printf("%d %0.2f ",p1->num,p1->score);
p1=p1->next;
}
printf("\n\npress enter end...");
getchar();
}
C语言版数据结构书上有一大把。