有没有大佬解释一下为什么第一段程序改成第二段程序运行就出错啊,跪谢。
一:
#include
#include
#include
typedef struct student{
char size[6];
float score;
struct student* next;
} Student;
int main()
{
int n=1;
char input[7];
Student *head=NULL;
Student *current, *end;
printf("please input the size of the student No.%d\n",n);
while(gets(input)!=NULL && input[0]!='0')
{
current=(Student *)malloc(sizeof(Student));
if(head==NULL)
{
head=current;
} else {
end->next=current;
}
printf("please input the score of the student No.%d\n",n);
strcpy(current->size,input);
scanf("%f",¤t->score);
while(getchar()!='\n')
{
continue;
}
current->next=NULL;
n++;
printf("please input the size of the student No.%d\n",n);
end=current;
}
Student *headl=NULL;
headl=head;
if(headl==NULL)
{
printf("No list entered!!!!!!!!\n");
}
while( headl!=NULL )
{
printf("the student with a school number of %6s have a score of %4.2f\n",headl->size,headl->score);
headl=headl->next;
}
while( headl!=NULL )
{
free(headl);
headl->next=headl;
}
return 0;
}
二:
#include
#include
#include
typedef struct student{
char size[6];
float score;
struct student* next;
} Student;
Student *add( Student *head );
void printfl( Student *head );
void freel( Student *head );
int main()
{
Student *head=NULL, *headl=NULL;
head=add(head);
printfl(head);
free(head);
return 0;
}
Student *add( Student *head )
{
int n=1;
char input[7];
Student *headl=NULL;
headl=head;
Student *current, *end;
printf("please input the size of the student No.%d\n",n);
while(gets(input)!=NULL && input[0]!='0')
{
if(headl==NULL)
{
headl=current;
} else {
end->next=current;
}
current=(Student *)malloc(sizeof(Student));
printf("please input the score of the student No.%d\n",n);
strcpy(current->size,input);
current->next=NULL;
scanf("%f",¤t->score);
while(getchar()!='\n')
{
continue;
}
n++;
printf("please input the size of the student No.%d\n",n);
end=current;
}
return headl;
}
void printfl( Student *head )
{
Student *headl=NULL;
headl=head;
if(headl==NULL)
{
printf("No list entered!!!!!!!!\n");
}
while( headl!=NULL )
{
printf("the student with a school number of %6s have a score of %f\n",headl->size,headl->score);
headl=headl->next;
}
}
void freel( Student *head )
{
Student *headl=NULL;
headl=head;
while( headl!=NULL )
{
free(headl);
headl->next=headl;
}
}