#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;float score;struct student *next;};
int n;
struct student *creat()?/*这个函数用于创建链表,返回值为链表的头指针*/
{struct student *head;
struct student *p1,*p2;n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(\"%ld,%f\",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf(\"%ld,%f\",&p1->num,&p1->score);/*输入学生的学号和分数*/
}
p2->next=NULL;
return(head);
}
void print(struct student *head)/*这个函数用于输出这个链表*/
{ struct student *p;
printf(\"\\nNow,These %d records are:\\n\",n);
p=head;
if(head!=NULL)
do
{printf(\"%ld %5.1f\\n\",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
void main()
{ struct student *head;
head=creat();/*创建链表*/
print(head);/*输出链表*/
printf(\"\\n\");
}
这其实是C程序设计(第三版)(谭浩强)上的几个小程序,被我整合成一个程序,编译没错,但运行时输入数据后出现错误,真的很郁闷,望高手指点。 |