编辑代码

#include <stdio.h>
#include<stdlib.h>

typedef struct stud{
    char name[20];
    long num;
    int age;
    char sex;
    float score;
    struct stud *next;
} STU;
STU *head,*cur,*newp;
void new_record();
void list();

int main () {
    char ch;
    int flag=1;
    head=NULL;
    while(flag){
        printf("\n type 'E'or'e' to enter new record,");
        printf("\n type'L'or'l' to list all records:");
        ch=getchar();
        getchar();
        switch(ch){
            case 'E':
            case 'e':
                new_record();
                break;
            case 'L':
            case 'l':
                list();
                break;
            default:
                flag=0;
        }
    }
	return 0;
}

void new_record()
{
    char numstr[20];
    newp=(STU *)malloc(sizeof(STU));
    if(head==NULL){
        head=newp;
    }else{
        cur=head;
        while(cur->next != NULL){
            cur=cur->next;
        }
        cur->next=newp;
    }
    cur=newp;
    printf("\n enter name:");
    gets(cur->name);
    printf("\n enter num:");
    gets(numstr);
    cur->num=atol(numstr);
    printf("\n enter age:");
    gets(numstr);
    cur->age=atoi(numstr);
    printf("\n enter sex:");
    cur->sex=getchar();
    getchar();
    printf("\n enter score:");
    gets(numstr);
    cur->score=atof(numstr);
    cur->next=NULL;
}

void list()
{
    int i=0;
    if(head == NULL){
        printf("\n empty list.\n");
        return;
    }
    cur=head;
    do{
        printf("\n record number %d\n",++i);
        printf("name:%s\n",cur->name);
        printf("num:%ld\n",cur->num);
        printf("age:%d\n",cur->age);
        printf("sex:%c\n",cur->sex);
        printf("score:%6.2f\n",cur->score);
        cur=cur->next;
    }while(cur!=NULL);
}