急,求高手指点下动态链表为什么会出错,谢谢!
#include<stdio.h>#include<stdlib.h>
typedef struct Student
{
int No;
int num;
float score;
struct Student *pnext;
}NODE,*PNODE;
PNODE creat(void);
void print(PNODE head);
void del(PNODE head);
void insert(PNODE head);
int main(void)
{
PNODE head;
head=creat();
return 0;
}
PNODE creat(void)
{
int i=0;
PNODE head,p,pnew;
head=(PNODE)malloc(sizeof(NODE));
if(head==NULL)
{
printf("动态内存分配失败!\n");
exit(-1);
}
head->pnext=NULL;
p=head;
do
{
pnew=(PNODE)malloc(sizeof(NODE));
i++;
if(pnew==NULL)
{
printf("动态内存分配失败!\n");
exit(-1);
}
printf("请输入学号及成绩:");
scanf("%d %f",&pnew->num,&pnew->score);
pnew->No=i;
if(pnew->num==0)//当学号为0时退出循环
{
p->pnext=NULL;
free(pnew);
}
else
{
p->pnext=pnew;
p=pnew;
}
}while(p->pnext!=NULL);
return head;
}
[此贴子已经被作者于2016-12-7 22:26编辑过]