C语言链表综合差错。崩溃中...(谭浩强书上的)

来源:百度知道 编辑:UC知道 时间:2024/06/07 08:21:50
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
#define NULL 0
struct student
{
int num;
float score;
struct student *next;
};
int n;
void print(struct student *head)
{
struct student *p;
printf("\nNow the %d records are:\n",n);
p=head;
if(head!=NULL)
do{
printf("%7d %5.2f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
struct student *creat()
{
struct student *head,*p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p1=(struct student *)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}

#include<stdio.h>
#include<malloc.h>

#ifndef NULL
#define NULL 0
#endif

struct student
{
int num;
float score;
student *next;
};
int n=0;
void print(student *head)
{
student *p=head;
printf("\nNow the %d records are:\n",n);

while (p){
printf("%7d %5.2f\n",p->num,p->score);
p=p->next;
};
}
student *creat()
{
student *head=0,*p0=0,*p1=0;
n=0;
int inum=0;
float fscore=0.0f;
while(true)
{
scanf("%d %f",&inum,&fscore);
if (inum==0) break;

p1=(student *)malloc(sizeof(student));
++n;
p1->num=inum;p1->score=fscore;p1->next=0;
if (p0) {
p0->next=p1;
p0=p1;

}else{
head=p0=p1;

}
}
return head;
}