链表的基本操作

发布时间 2024-01-05 11:46:57作者: merak_lbz

以PTA的7-61为例

创建学生信息管理系统,具体要求如下:

学生信息包括:学号 姓名 数学成绩 英语成绩 计算机成绩

功能1:添加学生信息
执行1时,输入学号,姓名,三门科目成绩;如果添加学生成功则输出“Add success”,如果学生已存在则输出“Students already exist”

功能2:删除学生信息
执行2时,输入学号信息;如果学生不存在,输出“Students do not exist”,如果存在,则输出“Delete success”

功能3:更改学生成绩信息
执行3时,输入学号信息;如果学生不存在,输出“Students do not exist”,如果存在,输出“Update success”

功能4:显示学生平均分成绩
执行4时,输入学号信息;如果学生不存在,输出“Students do not exist”,如果存在,则输出学生信息,如下格式:

Student ID:2019989890

Name:Jerry

Average Score:89.3

其中平均分为三门科目相加除以3,保留一位小数,每行之间换行。

输入格式:
第一行为一个整数n(0<n<130),后边共n行,每一行表示执行一种功能。其中1,2,3,4分别对应执行上面4种功能,具体格式见输入样例。
测试用例保证:学号和名字均为长度不超过10的字符串,各门课成绩为0到100之间的整数。

输出格式:
输入样例:
8
1 201817123 Tom 89 80 76
1 2019989890 Jerry 78 99 67
4 201817123
2 201817123
4 201817123
4 2019989890
3 2019989890 79 90 99
4 2019989890

输出样例:
Add success
Add success
Student ID:201817123
Name:Tom
Average Score:81.7
Delete success
Students do not exist
Student ID:2019989890
Name:Jerry
Average Score:81.3
Update success
Student ID:2019989890
Name:Jerry
Average Score:89.3

代码块如下

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct score{
    char name[11];
    char id[11];
    int math;
    int english;
    int cs;
    struct score *next;
}list;
list * creat(){
    list *head=(list*)malloc(sizeof(list));
    head->next=NULL;
    return head;
}
void ADD(list* head){
    char name[11],id[11];
    int sig=0;
    int english,math,cs;
    list* node=head;
    scanf("%s%s%d%d%d",id,name,&math,&english,&cs);
    while(node->next!=NULL){
        node=node->next;
        if(!strcmp(id,node->id)){
            printf("Students already exist\n");
            sig=1;
            break;
        }
    }
    if(sig==0){
    list* new_node=(list*)malloc(sizeof(list));
    node->next=new_node;
    new_node->next=NULL;
    new_node->english=english;
    new_node->math=math;
    new_node->cs=cs;
    strcpy(new_node->name,name);
    strcpy(new_node->id,id);
    printf("Add success\n");
    }
    
}
void Delete(list* head){
    char id[11];
    int sig=0;
    scanf("%s",id);
    list *last,*node=head;
    while(node->next!=NULL){
        last=node;
        node=node->next;
        if(!strcmp(node->id,id)){
            last->next=node->next;
            free(node);
            printf("Delete success\n");
            sig=1;
            break;
        }
    }
    if(sig==0)
        printf("Students do not exist\n");
}
void reform(list* head){
    char id[11];
    int math,english,cs;
    int sig=0;
    list* node=head;
    scanf("%s%d%d%d",id,&math,&english,&cs);
    while(node->next!=NULL){
        node=node->next;
        if(!strcmp(node->id,id)){
            node->math=math;
            node->english=english;
            node->cs=cs;
            printf("Update success\n");
            sig=1;
            break;
        }
    }
    if(sig==0)
        printf("Students do not exist\n");
    
}
void echo(list* head){
    list* node=head;
    int sig=0;
    char id[11];
    scanf("%s",id);
    while(node->next!=NULL){
        node=node->next;
        if(!strcmp(id,node->id)){
            printf("Student ID:%s\n",id);
            printf("Name:%s\n",node->name);
            float average=(node->math+node->english+node->cs)/3.0;
            printf("Average Score:%.1f\n",average);
            sig=1;
            break;
        }
    }
    if(sig==0)
        printf("Students do not exist\n");
}
int main(){
    int n;
    scanf("%d",&n);
    int i,j;
    int perform;
    list *head=creat();
    for(i=0;i<n;i++){
        scanf("%d",&perform);
        if(perform==1)
            ADD(head);
        if(perform==2)
            Delete(head);
        if(perform==3)
            reform(head);
        if(perform==4)
            echo(head);
    }
    return 0;
}