建立一个动态链表,为什么不行,帮我看看,非常感谢!!!
#include<stdio.h>#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct people)
struct people
{char id[20];
char name[20];
char sex[10];
int birth;
struct people *next;
};
int n;
struct people *creat(void)
{struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people *)malloc(LEN);
scanf("%s%s%s%d",p1->id,p1->name,p1->sex,&p1->birth);
head=NULL;
while(p1->id!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people *)malloc(LEN);
scanf("%s%s%s%d",p1->id,p1->name,p1->sex,&p1->birth);
}
p2->next=NULL;
return(head);
}
void print(struct people *head)
{struct people *p;
printf("these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%s %s %s %d",p->id,p->name,p->sex,p->birth);
p=p->next;
}while(p!=NULL);
}
void main()
{struct people *head;
printf("please input records:\n");
head=creat();
print(head);
}