[求助]学数据结构写了个小程序,但是怎么都调不过,求解
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
typedef struct Student{
int id;
int age;
char name[20];
int grade;
char sex;
int index;
struct Student * sNext;
}stu,*pstu;
void init(pstu pHead,pstu pTail);
void input(pstu pHead,pstu pTail);
void traverse(pstu pHead);
int main(){
stu sHead;
stu sTail;
init(&sHead,&sTail);
input(&sHead,&sTail);
traverse(&sHead);//一到这个遍历函数,程序就挂了,求解?
return 0;
}
//构造头结点
void init(pstu pHead,pstu pTail){
pHead=(pstu)malloc(sizeof(stu));
pTail=pHead;
pHead->index=0;
pHead->sNext=NULL;
}
//添加
void input(pstu pHead,pstu pTail){
pstu p=(pstu)malloc(sizeof(stu));
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入学号:");
scanf("%d",&p->id);
printf("请输入年龄:");
scanf("%d",&p->age);
printf("请输入成绩:");
scanf("%d",&p->grade);
p->index=pTail->index+1;
pTail->sNext=p;
pTail=p;
pTail->sNext=NULL;
}
//遍历
void traverse(pstu pHead){
if(pHead==NULL){
printf("你还没有存储任何学生信息!!");
return;
}
pstu p;
p=pHead->sNext;
while(p!=NULL){
printf("编号:%d||姓名:%s||学号:%d||年龄:%d||成绩:%d",
p->index,p->name,p->id,p->age,p->grade);
p=p->sNext;
}
return;
}