题目:有两个链表a和b,设结点中包含学号和姓名。从a链表中删去与b链表中有相同学号的那些结点。
// 删除相同的结点.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#define NULL 0
struct student
{
int num;
char *name;
struct student *next;
};
int main(int argc, char* argv[])
{
struct student a[3],b[3],*head,*tp,*temp;
a[0].num=1; a[0].name="曾小贤"; a[0].next=&a[1];
a[1].num=2; a[1].name="黄渤"; a[1].next=&a[2];
a[2].num=3; a[2].name="罗大佑"; a[2].next=NULL;
b[0].num=2; b[0].name="黄渤"; b[0].next=&b[1];
b[1].num=3; b[1].name="罗大佑"; b[1].next=&b[2];
b[2].num=4; b[2].name="张飞"; b[2].next=NULL;
head=&a[0];
temp=head;
do
{
for(int L=0;L<=2;L++)
{
if(temp->num==b[L].num)
{
if(temp==head)
{
head=temp->next;
}
else if(temp->next==NULL)
{
tp->next=NULL;
}
else
{
tp->next=temp->next;
}
}
}
tp=temp;
temp=temp->next;
}while(temp!=NULL);
do
{
printf("%d,%s\n",head->num,head->name);
head=head->next;
}while(head!=NULL);
return 0;
}
结果有误,希望广大读者能帮我分析一下。