2024.8.16~18

单链表实现学生管理系统(参照顺序表技能)写出菜单界面switch选择,功能1创建单链表,2录入学生信息,3删除一个学生信息,4按照成绩修改一个学生的年龄,5、按照姓名顺序查找一个学生是否存在。要求: 1、学生信息结构体姓名,分数,年龄,电话。2、其他功能函数自定义,分文件编译。

main.c

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

// 主菜单
void menu() {
    printf("\nStudent Management System\n");
    printf("1. Create Linked List\n");
    printf("2. Add Student\n");
    printf("3. Delete Student\n");
    printf("4. Modify Student Age by Score\n");
    printf("5. Find Student by Name\n");
    printf("6. Display All Students\n");
    printf("0. Exit\n");
}

int main() {
    Student *head = NULL;
    int choice, age;
    float score;
    Student student;
    char name[20];

    while (1) {
        menu();
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                head = createList();
                printf("Linked List created.\n");
                break;
            case 2:
                printf("Enter name, score, age, phone: ");
                scanf("%s %f %d %s", student.name, &student.score, &student.age, student.phone);
                addStudent(&head, student);
                printf("Student added.\n");
                break;
            case 3:
                printf("Enter name to delete: ");
                scanf("%s", name);
                deleteStudent(&head, name);
                break;
            case 4:
                printf("Enter score to modify: ");
                scanf("%f", &score);
                printf("Enter new age: ");
                scanf("%d", &age);
                modifyStudentAgeByScore(head, score, age);
                break;
            case 5:
                printf("Enter name to find: ");
                scanf("%s", name);
                if (findStudentByName(head, name)) {
                    printf("Student found: %s\n", name);
                } else {
                    printf("Student not found.\n");
                }
                break;
            case 6:
                printAllStudents(head);
                break;
            case 0:
                printf("Exiting...\n");
                while (head != NULL) {
                    Student *temp = head;
                    head = head->next;
                    free(temp);
                }
                return 0;
            default:
                printf("Invalid choice!\n");
        }
    }
}

main.h

#ifndef STUDENT_LIST_H
#define STUDENT_LIST_H

typedef struct Student {
    char name[20];
    float score;
    int age;
    char phone[15];
    struct Student *next;
} Student;

Student* createList();
void addStudent(Student **head, Student student);
void deleteStudent(Student **head, const char *name);
void modifyStudentAgeByScore(Student *head, float score, int newAge);
int findStudentByName(Student *head, const char *name);
void printAllStudents(Student *head);

#endif

func.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"

// 创建单链表
Student* createList() {
    return NULL;
}

// 录入学生信息
void addStudent(Student **head, Student student) {
    Student *newStudent = (Student*)malloc(sizeof(Student));
    *newStudent = student;
    newStudent->next = *head;
    *head = newStudent;
}

// 删除学生信息
void deleteStudent(Student **head, const char *name) {
    Student *temp = *head, *prev = NULL;

    while (temp != NULL && strcmp(temp->name, name) != 0) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("Student not found!\n");
        return;
    }

    if (prev == NULL) {
        *head = temp->next;
    } else {
        prev->next = temp->next;
    }

    free(temp);
    printf("Student deleted.\n");
}

// 按照成绩修改学生的年龄
void modifyStudentAgeByScore(Student *head, float score, int newAge) {
    Student *temp = head;

    while (temp != NULL) {
        if (temp->score == score) {
            temp->age = newAge;
            printf("Age updated for student %s.\n", temp->name);
            return;
        }
        temp = temp->next;
    }
    printf("Student with score %.2f not found!\n", score);
}

// 按照姓名查找学生信息
int findStudentByName(Student *head, const char *name) {
    Student *temp = head;

    while (temp != NULL) {
        if (strcmp(temp->name, name) == 0) {
            return 1;
        }
        temp = temp->next;
    }
    return 0;
}

// 打印所有学生信息
void printAllStudents(Student *head) {
    Student *temp = head;

    while (temp != NULL) {
        printf("Name: %s, Score: %.2f, Age: %d, Phone: %s\n", temp->name, temp->score, temp->age, temp->phone);
        temp = temp->next;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值