我照书打了这个程序,怎么有错误?谭浩强,张基温编的C书本~
用链表存放学生数据:
#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("\ntype'L'or'l'to list all records:");
ch=getchar();getchar();
switch(ch)
{ case'e':
case'E':new_record();break;
case'l':
case'L':listall();break;
default:flag=0;}
}
}
void new_record(void)
{
int 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("\nenter name:");
gets(this->name);
printf("\nenter number:");
gets(numstr);
this->num=atol(numstr);
printf("\nenter age:");
gets(numstr);
this->age=atoi(numstr);
printf("\nenter sex:");
this->sex=getchar();getchar();
printf("\nenter score:");
gets (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);
}
朋友们帮我看看~~
turboc 2.0编译文件http://wenmin311.go.nease.net/wenmin.rar
[此贴子已经被作者于2004-06-02 12:12:47编辑过]