链表问题有点困惑~~~~~~~~
#include<stdio.h>#include<stdlib.h>
#define NULL ((void *)0)
#define TYPE struct stu
#define LEN sizeof (struct stu)
struct stu
{
int id;
char name[20];
float grade;
struct stu *next;
};
TYPE *creat(int n) /*创造n个结点的指针函数*/
{
struct stu *head,*pf,*pb;
int i;
printf("Please input id,name,grade\n");
for(i=0;i<n;i++)
{
pb=(TYPE *) malloc(LEN);
scanf("%d",&pb->id);
gets(pb->name);
scanf("%f",&pb->grade);
if(i==0)
pf=head=pb;
else
pf->next=pb;
pb->next=NULL;为什么要把下一结点的地址赋空值?~~
pf=pb;
}
return(head);
}
void print(TYPE *head) /*输出函数*/
{
printf("please input id,name,grade\n");
while(head!=NULL)
{
printf("%4d%8s%8.1f\n",head->id,head->name,head->grade);
head=head->next;
}
}
void main()
{
int n;
TYPE *head;
printf("Please input n\n");
scanf("%d",&n);
head=creat(n);
print(head);
}
黑体部分为什么改成#define NULL 0我的系统是XP的,装的是VC6.0就报错了啊~~