链表赋值
//运行到给名字成员赋值时就出错了。源代码如下:
#include "stdio.h"
#include "stdlib.h"
struct stud
{
char name[20];
int age;
int num;
float score;
struct stud *next;
};
struct stud *head,*current,*news;
void ListAll(void);
void EnterNew(void);
main()
{
char ch;
int flag=1;
while(flag)
{
printf("E to enter new\nL to list all\n");
ch=getchar();getchar();
switch(ch)
{
case'e':
case'E':
EnterNew();
break;
case'l':
case'L':
ListAll();
break;
default:flag=0;
}
}
}
void EnterNew(void)//append new data
{
news=(struct stud *)malloc(sizeof(struct stud));
if(head==NULL)
{
head=news;
printf("%s",head->name);
}
else
{
current=head;
while(current->next!=NULL)
current=current->next;
current->next=news;
}
printf("\nEnter name:");
gets(current->name); //error here!!!
current->next=NULL;
}
void ListAll(void)//list the data
{
}