C语言。有两个链表a和b,从a链表中删去与b链表中有相同学号的那些结点!

#include <stdio.h>
#include <malloc.h>

struct student
{
int num;
char name[10];
struct student *next;

};

void main()
{ void print(struct student *head);
struct student *creat();
struct student *solve(struct student *head_a,struct student *head_b);
struct student *head,*head_a,*head_b;
head_a=creat();
head_b=creat();

head=solve(head_a,head_b);
print(head);

}

struct student *creat()
{
struct student *head,*p,*p1;
p1=(struct student *)malloc(sizeof(struct student));
head=p=p1;
printf("输入学号和姓名0停止接收数据\n");
printf("请输入学生学号和姓名:");
scanf("%d %s",&p->num,&p->name);
while(p->num!=0)
{
printf("请输入学生学号和姓名:");

p1->next=p;
p1=p;
p=(struct student *)malloc(sizeof(struct student));
scanf("%d %s",&p->num,&p->name);

}

p1->next=NULL;
return head;
}

struct student *solve(struct student *head_a,struct student *head_b)
{
struct student *p,*p1,*t;
p=p1=head_a;
t=head_b;
while(p!=NULL)
{
t=head_b;
while(t->next!=NULL&&(t->num!=p->num))
{

t=t->next;

}
if(t->num==p->num)
{
if(p==head_a)
{
head_a=head_a->next;
p1=p=head_a;
}
else
{
p1->next=p->next;
p=p->next;
}

}
else
{
p1=p;
p=p->next;
}

}
return head_a;
}

void print(struct student *head)
{
struct student *p;

while(p!=NULL)
{
printf("%d %s\n",p->num,p->name);
p=p->next;

}

}

帮我看看我哪里写错了。。。。。答的好有追加~

第1个回答  2011-09-14
检查过了,你的程序基本正确就是print里边需要一点:
void print(struct student *head)
{
struct student *p = head; // 初始化

while(p!=NULL)
{
printf("%d %s\n",p->num,p->name);
p=p->next;

}

}
第2个回答  2011-09-14
Creat函数中的while一段有点问题:应改为:
while(p->num!=0)
{
printf("请输入学生学号和姓名:");
p=(struct student *)malloc(sizeof(struct student));
p1->next=p;
p1=p;
scanf("%d %s",&p->num,&p->name);

}
第3个回答  2011-09-14
g
相似回答