C语言线性链表学生管理系统总分排名
时间: 2023-12-30 07:05:48 浏览: 45
以下是一个C语言线性链表学生管理系统总分排名的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
int num; // 学号
char name[20]; // 姓名
int score; // 分数
int total; // 总分
struct student *next; // 下一个学生节点
} Student;
// 创建学生节点
Student *createStudent(int num, char *name, int score) {
Student *newStudent = (Student *)malloc(sizeof(Student));
newStudent->num = num;
strcpy(newStudent->name, name);
newStudent->score = score;
newStudent->total = 0;
newStudent->next = NULL;
return newStudent;
}
// 添加学生节点到链表尾部
void addStudent(Student **head, Student *newStudent) {
if (*head == NULL) {
*head = newStudent;
} else {
Student *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
}
// 计算学生总分
void calculateTotal(Student *head) {
Student *current = head;
while (current != NULL) {
current->total = current->score;
current = current->next;
}
}
// 根据总分对学生进行排名
void rankStudents(Student *head) {
Student *current = head;
while (current != NULL) {
Student *temp = current->next;
while (temp != NULL) {
if (temp->total > current->total) {
// 交换学生节点位置
int num = current->num;
char name[20];
strcpy(name, current->name);
int score = current->score;
int total = current->total;
current->num = temp->num;
strcpy(current->name, temp->name);
current->score = temp->score;
current->total = temp->total;
temp->num = num;
strcpy(temp->name, name);
temp->score = score;
temp->total = total;
}
temp = temp->next;
}
current = current->next;
}
}
// 打印学生信息及排名
void printStudents(Student *head) {
Student *current = head;
int rank = 1;
while (current != NULL) {
printf("Rank: %d, Num: %d, Name: %s, Score: %d, Total: %d\n", rank, current->num, current->name, current->score, current->total);
rank++;
current = current->next;
}
}
int main() {
Student *head = NULL;
// 添加学生节点
addStudent(&head, createStudent(1, "Alice", 80));
addStudent(&head, createStudent(2, "Bob", 90));
addStudent(&head, createStudent(3, "Charlie", 85));
// 计算学生总分
calculateTotal(head);
// 根据总分排名
rankStudents(head);
// 打印学生信息及排名
printStudents(head);
return 0;
}
```