c语言学生成绩管理系统源代码,要求使用链表
时间: 2023-12-05 11:05:36 浏览: 105
以下是一个简单的使用链表实现的C语言学生成绩管理系统的源代码,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct student {
char name[MAX_NAME_LEN];
int score;
struct student *next;
} Student;
Student *head = NULL;
void add_student() {
Student *new_student = (Student*)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生成绩:");
scanf("%d", &(new_student->score));
new_student->next = NULL;
if (head == NULL) {
head = new_student;
} else {
Student *ptr = head;
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = new_student;
}
printf("添加成功!\n");
}
void print_students() {
if (head == NULL) {
printf("没有学生记录!\n");
return;
}
printf("姓名\t成绩\n");
Student *ptr = head;
while (ptr != NULL) {
printf("%s\t%d\n", ptr->name, ptr->score);
ptr = ptr->next;
}
}
void search_student() {
if (head == NULL) {
printf("没有学生记录!\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
Student *ptr = head;
while (ptr != NULL) {
if (strcmp(ptr->name, name) == 0) {
printf("%s的成绩是%d\n", name, ptr->score);
return;
}
ptr = ptr->next;
}
printf("没有找到%s的记录!\n", name);
}
void delete_student() {
if (head == NULL) {
printf("没有学生记录!\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
if (strcmp(head->name, name) == 0) {
Student *temp = head;
head = head->next;
free(temp);
printf("删除成功!\n");
return;
}
Student *ptr = head;
while (ptr->next != NULL) {
if (strcmp(ptr->next->name, name) == 0) {
Student *temp = ptr->next;
ptr->next = temp->next;
free(temp);
printf("删除成功!\n");
return;
}
ptr = ptr->next;
}
printf("没有找到%s的记录!\n", name);
}
int main() {
while (1) {
printf("请选择要执行的操作:\n");
printf("1.添加学生\n");
printf("2.显示所有学生\n");
printf("3.查找学生\n");
printf("4.删除学生\n");
printf("5.退出程序\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
print_students();
break;
case 3:
search_student();
break;
case 4:
delete_student();
break;
case 5:
exit(0);
default:
printf("无效的选择,请重新输入!\n");
}
}
return 0;
}
```
这个学生成绩管理系统使用链表来存储学生记录,可以动态添加、查找和删除学生记录,并可以显示所有学生记录。用户可以通过命令行菜单来选择要执行的操作。
阅读全文