学生信息管理系统

设计一个学生信息管理系统,学生信息包括:学号,姓名,专业,住址,电话。试编写满足以下要求的程序,并调试通过。

要求:系统能够增加、删除、修改学生的信息记录,且能够根据学号查找该学生的住址,电话。

cs 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 学生信息结构体
typedef struct Student {
    int id;   // 学号 
    char name[10];   // 姓名 
    char major[20];   // 专业
    char address[50];   // 住址
    char phone[20];   // 电话
} Student;

// 单链表节点结构体
typedef struct Node {
    Student data;
    struct Node *next;
} Node;

// 单链表头指针
Node *head = NULL;

// 函数声明
void output();
void insert();
void deleteById(int id);
void modifyById(int id);
void searchById(int id);
void printList();
void addToList(Student student);
void removeFromList(int id);
void initializeStudents();  // 初始化已有的学生信息

// 设定已有5名学生信息
void initializeStudents() {
    Student students[5] = {
        {2301, "小陈", "计算机", "翻斗花园1号楼2号房", "1008611"},
        {2302, "小梁", "软件学", "翻斗花园2号楼5号房", "1008622"},
        {2303, "小吴", "审计", "翻斗花园3号楼9号房", "1008633"},
        {2304, "小李", "文学", "不翻斗草园2号楼2号房", "1008644"},
        {2305, "小黄", "密码学", "不翻斗草园2号楼3号房", "1008655"} 
    };

    for (int i = 0; i < 5; i++) {
        addToList(students[i]);  // 将学生信息添加到单链表
    }
}

int main() {
    int choice, id;

    // 初始化5名学生
    initializeStudents();

    while (1) {
        printf("\n学生信息管理系统\n");
        printf("1. 输出学生信息\n");
        printf("2. 插入学生信息\n");
        printf("3. 删除学生信息\n");
        printf("4. 修改学生信息\n");
        printf("5. 查找学生信息\n");
        printf("6. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                output();
                break;
            case 2:
                insert();
                break;
            case 3:
                printf("请输入要删除的学生学号: ");
                scanf("%d", &id);
                deleteById(id);
                break;
            case 4:
                printf("请输入要修改的学生学号: ");
                scanf("%d", &id);
                modifyById(id);
                break;
            case 5:
                printf("请输入要查找的学生学号: ");
                scanf("%d", &id);
                searchById(id);
                break;
            case 6:
                return 0;
            default:
                printf("无效的选择,请重新选择!\n");
        }
    }

    return 0;
}

// 输出学生信息
void output() {
    printf("\n单链表中的学生信息表:\n");
    printf("学号\t姓名\t专业\t住址\t电话\n");
    printList(); // 打印单链表中的学生信息
}

// 插入学生信息
void insert() {
    Student newStudent;
    printf("请输入学号: ");
    scanf("%d", &newStudent.id);
    printf("请输入姓名: ");
    scanf("%s", newStudent.name);
    printf("请输入专业: ");
    scanf("%s", newStudent.major);
    printf("请输入住址: ");
    scanf("%s", newStudent.address);
    printf("请输入电话: ");
    scanf("%s", newStudent.phone);

    // 将新学生信息添加到单链表中
    addToList(newStudent);
}

// 删除学生信息
void deleteById(int id) {
    // 从单链表中删除学生信息
    removeFromList(id);
}

// 修改学生信息
void modifyById(int id) {
    Node *temp = head;
    while (temp != NULL) {
        if (temp->data.id == id) {
            printf("请输入新的姓名: ");
            scanf("%s", temp->data.name);
            printf("请输入新的专业: ");
            scanf("%s", temp->data.major);
            printf("请输入新的住址: ");
            scanf("%s", temp->data.address);
            printf("请输入新的电话: ");
            scanf("%s", temp->data.phone);
            return;
        }
        temp = temp->next;
    }

    printf("未找到该学生。\n");
}

// 查找学生信息
void searchById(int id) {
    Node *temp = head;
    while (temp != NULL) {
        if (temp->data.id == id) {
            printf("学号: %d 姓名: %s 专业: %s 住址: %s 电话: %s\n",
                   temp->data.id, temp->data.name, temp->data.major, temp->data.address, temp->data.phone);
            return;
        }
        temp = temp->next;
    }

    printf("未找到该学生。\n");
}

// 打印单链表中的学生信息
void printList() {
    Node *temp = head;
    while (temp != NULL) {
        printf("%d\t%s\t%s\t%s\t%s\n",
               temp->data.id, temp->data.name,
               temp->data.major, temp->data.address, temp->data.phone);
        temp = temp->next;
    }
}

// 将学生信息添加到单链表
void addToList(Student student) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = student;
    newNode->next = NULL;

    if (head == NULL) {
        head = newNode;
    } else {
        Node *temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

// 从单链表中删除学生信息
void removeFromList(int id) {
    Node *current = head;
    Node *previous = NULL;

    while (current != NULL && current->data.id != id) {
        previous = current;
        current = current->next;
    }

    if (current == NULL) {
        printf("没有找到该学生\n");
    } else if (previous == NULL) {
        head = current->next;
        free(current);
    } else {
        previous->next = current->next;
        free(current);
    }
}
相关推荐
阿卡好可爱25 分钟前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
一起学安全技术37 分钟前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
三雷科技41 分钟前
第6章第4节:自定义分词器
开发语言·c#
Delightrs2 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信150413057592 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
zhanghaha13142 小时前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#
GoAI13 小时前
DeepSeek Harness Memory (一)主流记忆插件总结
后端·算法·面试
Pniubi14 小时前
力扣54.螺旋矩阵
算法·leetcode·矩阵
Zwawa14 小时前
用两个按键做一个OLED菜单:先把选择和执行分开
c语言·单片机·嵌入式