班级成绩管理系统 (1)要求利用链表指针来操作完成、动态内存分配。(2)姓名、性别、程序设计训练课成绩。 (3)对成绩排序,同时可以插入学生、删除学生
时间: 2024-01-13 12:04:28 浏览: 74
操作系统课程设计-动态分区分配存储管理-报告+VS2019源码工程-C
5星 · 资源好评率100%
你好!对于班级成绩管理系统的要求,我可以给出一个基本的实现思路。首先,你可以定义一个学生结构体,包含姓名、性别和程序设计训练课成绩等字段。然后,你可以使用链表来存储学生信息,并通过指针进行操作。
以下是一个简单的示例代码,帮助你了解如何实现这个系统:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 学生结构体
typedef struct Student {
char name[20];
char gender[10];
int score;
struct Student* next;
} Student;
// 创建学生节点
Student* createStudentNode(char name[], char gender[], int score) {
Student* newNode = (Student*)malloc(sizeof(Student));
strcpy(newNode->name, name);
strcpy(newNode->gender, gender);
newNode->score = score;
newNode->next = NULL;
return newNode;
}
// 插入学生
void insertStudent(Student** head, char name[], char gender[], int score) {
Student* newNode = createStudentNode(name, gender, score);
if (*head == NULL) {
*head = newNode;
} else {
Student* curr = *head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newNode;
}
}
// 删除学生
void deleteStudent(Student** head, char name[]) {
if (*head == NULL) {
printf("链表为空,无法删除学生。\n");
return;
}
Student* prev = NULL;
Student* curr = *head;
while (curr != NULL) {
if (strcmp(curr->name, name) == 0) {
if (prev == NULL) {
*head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("已删除学生:%s\n", name);
return;
}
prev = curr;
curr = curr->next;
}
printf("未找到学生:%s\n", name);
}
// 成绩排序
void sortStudents(Student** head) {
if (*head == NULL || (*head)->next == NULL) {
printf("链表为空或只有一个学生,无需排序。\n");
return;
}
int swapped;
Student* curr;
Student* last = NULL;
do {
swapped = 0;
curr = *head;
while (curr->next != last) {
if (curr->score > curr->next->score) {
// 交换相邻两个节点的位置
int tmpScore = curr->score;
curr->score = curr->next->score;
curr->next->score = tmpScore;
char tmpName[20];
strcpy(tmpName, curr->name);
strcpy(curr->name, curr->next->name);
strcpy(curr->next->name, tmpName);
char tmpGender[10];
strcpy(tmpGender, curr->gender);
strcpy(curr->gender, curr->next->gender);
strcpy(curr->next->gender, tmpGender);
swapped = 1;
}
curr = curr->next;
}
last = curr;
} while (swapped);
}
// 打印学生信息
void printStudents(Student* head) {
if (head == NULL) {
printf("链表为空。\n");
return;
}
Student* curr = head;
while (curr != NULL) {
printf("姓名:%s,性别:%s,成绩:%d\n", curr->name, curr->gender, curr->score);
curr = curr->next;
}
}
// 释放链表内存
void freeStudents(Student** head) {
Student* curr = *head;
while (curr != NULL) {
Student* temp = curr;
curr = curr->next;
free(temp);
}
*head = NULL;
}
int main() {
Student* head = NULL;
// 插入学生信息
insertStudent(&head, "Alice", "Female", 85);
insertStudent(&head, "Bob", "Male", 92);
insertStudent(&head, "Charlie", "Male", 78);
insertStudent(&head, "Diana", "Female", 88);
// 打印学生信息
printf("初始学生信息:\n");
printStudents(head);
// 删除学生
deleteStudent(&head, "Bob");
// 打印学生信息
printf("删除学生后的信息:\n");
printStudents(head);
// 成绩排序
sortStudents(&head);
// 打印学生信息
printf("排序后的信息:\n");
printStudents(head);
// 释放内存
freeStudents(&head);
return 0;
}
```
这个示例代码实现了学生信息的插入、删除和排序功能。你可以根据自己的需求进行扩展和修改。希望能对你有所帮助!如果有任何问题,请随时提问。
阅读全文