用C语言编写一个学生成绩管理系统,要求是使用链表作为结构,实现插入,删除,修改和查询功能
时间: 2024-04-30 14:17:15 浏览: 101
纯C语言实现学生成绩管理系统,采用链表结构
5星 · 资源好评率100%
好的,我了解了你的要求。以下是用C语言实现学生成绩管理系统的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
char name[20];
int id;
float score;
struct student *next;
} Student;
// 新建学生节点
Student *new_student(char *name, int id, float score) {
Student *stu = (Student*)malloc(sizeof(Student));
strcpy(stu->name, name);
stu->id = id;
stu->score = score;
stu->next = NULL;
return stu;
}
// 插入学生节点
void insert_student(Student **head, Student *node) {
if (*head == NULL) {
*head = node;
return;
}
Student *cur = *head;
while (cur->next) {
cur = cur->next;
}
cur->next = node;
}
// 删除学生节点
void delete_student(Student **head, int id) {
if (*head == NULL) {
return;
}
if ((*head)->id == id) {
Student *tmp = *head;
*head = (*head)->next;
free(tmp);
return;
}
Student *cur = *head;
while (cur->next) {
if (cur->next->id == id) {
Student *tmp = cur->next;
cur->next = tmp->next;
free(tmp);
return;
}
cur = cur->next;
}
}
// 修改学生节点
void modify_student(Student **head, int id, float score) {
if (*head == NULL) {
return;
}
Student *cur = *head;
while (cur) {
if (cur->id == id) {
cur->score = score;
return;
}
cur = cur->next;
}
}
// 查询学生节点
void search_student(Student *head, int id) {
if (head == NULL) {
return;
}
Student *cur = head;
while (cur) {
if (cur->id == id) {
printf("学生姓名:%s,学号:%d,成绩:%.2f\n", cur->name, cur->id, cur->score);
return;
}
cur = cur->next;
}
}
int main() {
Student *head = NULL;
// 插入学生
insert_student(&head, new_student("张三", 1001, 80));
insert_student(&head, new_student("李四", 1002, 90));
insert_student(&head, new_student("王五", 1003, 85));
// 删除学生
delete_student(&head, 1002);
// 修改学生
modify_student(&head, 1001, 85);
// 查询学生
search_student(head, 1003);
return 0;
}
```
以上就是使用链表实现学生成绩管理系统的代码。注意,这里的代码只是一个简单的演示,实际应用中还需要完善其他功能,如输入输出、排序等。
阅读全文