我的C语言单链表错在哪里了啊

来源:百度知道 编辑:UC知道 时间:2024/06/24 13:00:33
#include<stdio.h>
#include <malloc.h>
typedef struct student{
int sno;
char name[20];
char gender[5];
int score;
}student;

typedef struct stu{
student student;
struct stu *next;
}stu;
stu *head;

void createlist(int n){
stu *p,*t;
int i;
head=(stu)malloc(sizeof(stu));
head->next=NULL;
p=head;
for(i=0;i<n;i++){
t=(stu)malloc(sizeof(stu));
scanf("%d,%s,%s,%d",t->student.sno,t->student.name,t->student.gender,t->student.score);
p->next=t;
t->next=NULL;
p=t;
}
}
void printlist(){
stu *p=head;
while(p->next!=NULL){
printf("sno:%d,name:%s,gener:%s,score:%d/n",p->student.sno,p->student.name,p->student.gender,p->student.score);
p=p->next;
}
}

void main(){
createlist(1);
printlist();
}

#include<stdio.h>
#include <malloc.h>
typedef struct student{
int sno;
char name[20];
char gender[5];
int score;
}student;

typedef struct stu{
student student;
struct stu *next;
}stu;
stu *head;

void createlist(int n){
stu *p,*t;
int i;
head=(stu *)malloc(sizeof(stu)); //要注意malloc函数返回值是一个指针,你强制转化的时候要转换成指向结构的指针啊!
head->next=NULL;
p=head;
for(i=0;i<n;i++){
t=(stu *)malloc(sizeof(stu)); //同上
scanf("%d,%s,%s,%d",t->student.sno,t->student.name,t->student.gender,t->student.score);
p->next=t;
t->next=NULL;
p=t;
}
}
void printlist(){
stu *p=head;
while(p->next!=NULL){
printf("sno:%d,name:%s,gener:%s,score:%d/n",p->student.sno,p->student.name,p->student.gender,p->student.score);
p=p->next;
}
}

void mai