求助:这个程序,从a链表中删除与b链表学号相同的元素。为什么运行时出错呢?
#include <iostream>using namespace std;
struct student
{
int num;
char name[10];
student *next;
}a[5]={{101,"feng"},{102,"wang"},{103,"li"},{104,"qiu"},{105,"tong"}},
b[3]={{101,"lian"},{102,"luo"},{103,"zhou"}};
int main()
{
int i;
student *head1,*head2,*p0,*p1,*p2;
head1=a;
head2=b;
for(p1=head1,i=1;i<5;i++)
{
p1->next=a+i;
p1=p1->next;
}
p1->next=NULL;
for(p2=head2,i=1;i<3;i++)
{p2->next=b+i;
p2=p2->next;
}
p2->next=NULL;
p0=p1=head1;
while(p1)
{
p2=head2;
while(p2!=NULL&&p1->num!=p2->num)
p2=p2->next;
if(p1->num==p2->num)
if(p1==head1)head1=head1->next;
else p0->next=p1->next;
p0=p1;
p1=p1->next;
} //如果注释了这一段,运行还是正常的...
p1=head1;
while(p1)
{cout<<p1->num<<' '<<p1->name<<endl;
p1=p1->next;
}
/*
p2=head2;
while(p2)
{cout<<p2->num<<' '<<p2->name<<endl;
p2=p2->next;
}*/
return 0;
}
这个程序哪里出错呢????多谢各位大侠!