|
我想用一个函数完成学生信息的录入。可以调用一次函数输入多组数据
以下是我的代码
struct student
{
char num[15];char name[20];float eng,math,cpro;struct student*next,*pre;
};
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define L sizeof(struct student)
#define NULL 0
struct student*head ,*tail;
int count=0;\\学生个数
void input(void)
{
struct student*p;
printf ("请输入学生的信息,依次为学号、姓名、英语成绩、数学成绩、c语言成绩。以学号0结束输入\n");
do
{
p=(struct student*)malloc(L);
scanf("%s%s%f%f%f",p->num,p->name,&p->eng,&p->math,&p->cpro);
count++;
if (count==1) {head=p;tail=p;p->next=NULL;p->pre=NULL;}
else
{
tail->next=p;p->pre=tail;p->next=NULL;tail=p;
}
}
while(strcmp(p->num,"0")!=0);
tail=tail->pre;
tail->next=NULL;
free(p);
}
由于我用的是do……while循环,所以每次调用函数输入结束后都有一个学号为0的空节点;所以每次调用函数都会多申请一个节点然后把它释放,效率很低。
但是用while循环,第一次输入时又不能判断
请问有什么好的方法吗? |
|