请帮我看看这个题到底错在哪里?提示有三处错。
题目(8) 用链表存放学生数据。用结构体数组来存放学生数据是静态存储方法,浪费内存空间。现在我们改用链表来处理,每一结点中存放一个学生的数据。程序由三个函数组成,new_record函数用来新增加一个结点,listall函数用来打印输出已有的全部结点中的数据。程序开始运行时若键入“E”或“e”则表示要进行增加新结点的操作,若键入“L”,或“l”,表示要输出所有结点中数据。
#include"stdlib.h"
#include"stdio.h"
struct stud
{
char name[20];
long num;
int age;
char sex;
float score;
struct stud*next;
};
struct stud*head,*this,*new;
main()
{
char ch;
int flag=1;
head=NULL;
while(flag)
{
printf("\ntype'E'or'e'to enter new record,");
printf("type'L'or'l'to list all record:");
ch=getchar();getchar();
switch(ch)
{
case'e':
case'E': new record();break;
case'l':
case'L': listall();break;
default:flag=0;
}/*end switch*/
}/*end while*/
}/*end main*/
void new_record(void)
{
char numstr[20];
new=(struct stud*)malloc(sizeof(struct stud));
if(head==NULL)
head=new;
else
{
this=head;
while(this->next!=NULL)
this=this->next;
this->next=new;
}
this=new;
printf("\enter name: ");
gets(this->name);
printf("\nenter numeber: ");
gets(numstr);
this->num=atoi(numstr);
printf("\nenter age: ");
gets(numstr);
this->age=atoi(numstr);
printf("\nenter sex: ");
this->sex=getchar();
getchar();
printf("\nenter score: ");
gets(numstr);
this->score=atof(numstr);
this->next=NULL;
}
void listall(void)
{
int i=0;
if(head==NULL)
{
printf("\nempty list.\n");
return;
}
this=head;
do
{
printf("nrecord number %d\n",++i);
printf("name:%s\n",this->name);
printf("num:%ld\n",this->num);
printf("age:%d\n",this->age);
printf("sex:%c\n",this->sex);
printf("score:%6.2f\n",this->score);
this=this->next;
}while(this!=NULL);
}
运行情况如下:
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:e↙
enter name:wangli↙
enter number:89101↙
enter age:18↙
enter sex:m↙
enter score:89.5↙
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:e↙
enter name:zhangfu↙
enter number:89102↙
enter age:19↙
enter sex:m↙
enter score:90.5↙
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:L↙
record number 1
name:wangli
num:89101
age:18
sex:m
score:89.50
record number 2
name:zhangfun
num:89102
age:19
sex:m
score:90.50
type ’E’or’e’ to enter new record,type ’L’or’l’ to list all record:c↙
http://single.xhblog.com/archives/2007/209693.shtml
[此贴子已经被作者于2007-8-25 8:42:40编辑过]