建立单链表
建立储存20个学生信息的单链表,我的程序如下:/* Note:Your choice is C IDE */
#include "stdio.h"
#include <malloc.h>
struct person{
long num;
char name[20];
char sex;
int age;
long tel;
}student;
typedef struct stu{
struct person student;
struct stu *next;
}linklist;
linklist *creatlist()
{
int i;
struct person x;
linklist *head,*p,*r;
p=(linklist*)malloc(sizeof(linklist));
head=p;
p->next=NULL;
r=p;
for(i=0;i<=20;i++)
{
scanf("%ld%c%c%d%ld",&x.num,&x.name[i],&x.sex,&x.age,&x.tel);
p=(linklist*)malloc(sizeof(linklist));
p->student.num=x.num;
p->student.name[i]=x.name[i];
p->student.sex=x.sex;
p->student.age=x.age;
p->student.tel=x.tel;
p->next=NULL;
r->next=p;
r=r->next;
}
return(head);
}
void main()
{
int i;
linklist *stdhead;
for(i=0;i<=20;i++)
{stdhead=creatlist();
printf("%ld,%c,%c,%d,%ld\n",stdhead->num,stdhead->name[i],stdhead->sex,stdhead->age,stdhead->tel);
}
}
请高手指正其中的错误,多谢多谢!