求求各位大虾帮忙看看这个问题啊。
题目:有2个链表a,b,设结点中包含学号、姓名。从a链表中删去和b链表中相同的学号的那些结点。
自己写的代码:
#include "stdio.h"
#include "malloc.h"
void print(struct student *head);
struct student *creat();
struct student *del(struct student *head,long num);
struct student
{
int num;
char name;
struct student *next;
};
int n;
void main()
{
struct student *p1,*p2,*p3;
p1=creat();
p2=creat();
for(p2;p2->next!=NULL;p2++)
p3=del(p1,p2->next->num);
print(p3);
}
struct student *creat()
{
struct student *head,*p1,*p2;
int n;
p1=p2=(struct student*)malloc(sizeof(struct student));
cin>>p1->num>>p1->name;
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
cin>>p1->num>>p1->name;
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
cout<<"Now,These "<<n<<"records are:";
if(head!=NULL)
{
do
{
cout<<p->num<<p->name;
p=p->next;
}while(p!=NULL);
}
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL){cout<<"List null";}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else p2->next=p1->next;
cout<<"delete "<<num;
n=n-1;
}
else cout<<num<<"not been found!";
return head;
}
运行和编译都通过了,但是自己逐步调试的时候发现运行到for(p2;p2->next!=NULL;p2++);的时候出现错误。自己主要是想通过这一句p2=creat();将num调出来,以便删除函数p3=del(p1,p2->next->num);能够删除p1=creat();中和p2=creat();相同num的结点,而达到题目的要求。