c语言的问题(代别人提问)

有两个链表,节点中包含学号和姓名。从a链表中删去与b链表中有相同学号的节点。(最好能帮我指出错误,谢谢了)
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(student)
int n;
struct student
{
long num;
char name[20];
struct student *next;
};
struct student* creat(void)
{ n=0;

struct student *p1,*p2,*head;
p1=p2=(struct student*)malloc(LEN);
head=NULL;
printf("please input the number of this student:");
scanf_s("%ld",&p1->num);
getchar();
printf("please input the name of this student:");
gets(p1->name);
if(p1->num!=0)
{
for(;p1->num!=0;)
{
n=n+1;
if(n==1){head =p1;}
else{p2->next=p1;}
p2=p1;
p1=(struct student*)malloc(LEN);
printf("please input the number of this student:");
scanf_s("%ld",&p1->num);
getchar();
if(p1->num==0)break;
printf("please input the name of this student:");
gets(p1->name);
}
}
p2->next=NULL;
return head;

}
void print(struct student *head)
{
struct student *p;
p=head;
if(p!=NULL)
{
do
{
printf("学号:%ld,姓名:%s\n",p->num,p->name);
p=p->next;
}while(p!=NULL);
}
}
struct student *del(struct student *ahead ,struct student *bhead)
{
struct student *a1,*a2,*b1;
if(ahead==NULL||bhead==NULL)
{printf("\nlist full\n");return ahead;}
a1=ahead;
b1=bhead;
while(b1->next!=NULL&&a1->next!=NULL)
{
while(b1->num!=a1->num&&a1->next!=NULL)
{a2=a1;a1=a1->next;}
if(b1->num==a1->num)
{
if(a1==ahead)
ahead=a1->next;
else
a2->next=a1->next;
printf("delete:%d\n",b1->num);
n=n-1;

}
else printf("%ld not been found!",b1->num);
b1=b1->next;
}
return ahead;
}
void main()
{
struct student * listA,* listB;
printf("please input listA:");
listA=creat();
printf("please input listB:");
listB=creat();
print(listA);
print(listB);
//del(listA,listB);
//printf("\n\n");
//print(listA);
//print(listB);
}

//你的程序写的很好,只有一处错误,已经改正,可以正确运行;
//要必须注意的是结构体指针跟一般数组指针不一样,数组地址作为形参时,
//会因为在调用函数体的操作而改变,而结构体指针不会,所以要传地址引用
//像我下面用的一样,我把所有的Struct student * 替换为Ptrstu就好了
//希望kutpbpb的回答能对你有所帮助!
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(student)
int n;
struct student
{
long num;
char name[20];
struct student *next;
};
typedef struct student* Ptrstu; //这里
Ptrstu creat(void)
{ n=0;

Ptrstu p1,p2,head;
p1=p2=(Ptrstu)malloc(LEN);
head=NULL;
printf("\ninput the number :");
scanf_s("%ld",&p1->num);
getchar();
printf("\nplease input name :");
gets(p1->name);
if(p1->num!=0)
{
for(;p1->num!=0;)
{
n=n+1;
if(n==1){head =p1;}
else{p2->next=p1;}
p2=p1;
p1=(Ptrstu)malloc(LEN);
printf("\nplease input the number:");
scanf_s("%ld",&p1->num);
getchar();
if(p1->num==0)break;
printf("\nplease input the name:");
gets(p1->name);
}
}
p2->next=NULL;
return head;

}
void print(Ptrstu head)
{
printf("\nPlease Out the List:");
Ptrstu p;
p=head;
if(p!=NULL)
{
do
{
printf("\n学号:%ld,姓名:%s\n",p->num,p->name);
p=p->next;
}while(p!=NULL);
}
}
Ptrstu del(Ptrstu &ahead ,Ptrstu &bhead)//错误基本上只有一处,这里,要传地址引用
{
Ptrstu a1,a2,b1;
if(ahead==NULL||bhead==NULL)
{printf("\nlist full\n");return ahead;}
a1=ahead;
b1=bhead;
while(b1->next!=NULL&&a1->next!=NULL)
{
while(b1->num!=a1->num&&a1->next!=NULL)
{a2=a1;a1=a1->next;}
if(b1->num==a1->num)
{
if(a1==ahead)
ahead=a1->next;
else
a2->next=a1->next;
printf("\ndelete:%d\n",b1->num);
free(a1); //这里,用完要记得释放
n=n-1;

}
else printf("\n%ld not been found!",b1->num);
b1=b1->next;
a1=ahead;
}
return ahead;
}
void main()
{
Ptrstu listA,listB;
printf("\nplease input listA:");
listA=creat();
printf("\nplease input listB:");
listB=creat();
print(listA);
print(listB);
del(listA,listB);
printf("\n\n");
print(listA);
print(listB);
}百度地图

本数据来源于百度地图,最终结果以百度地图最新数据为准。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-22
n=n+1;
if(n==1){head =p1;}
else{p2->next=p1;}
p2=p1;
p1=(struct student*)malloc(LEN);
printf("please input the number of this student:");
scanf_s("%ld",&p1->num);
getchar();
if(p1->num==0)break;
printf("please input the name of this student:");
gets(p1->name);本回答被提问者采纳
第2个回答  2010-05-22
结构体指针不会,所以要传地址引用
第3个回答  2010-05-04
有时间看看