学生管理系统有序链表
时间: 2023-11-30 13:42:43 浏览: 63
学生管理系统有序链表是一种基于链表的数据结构,它可以按照一定的规则对学生信息进行排序,使得查询、添加、删除等操作更加高效。下面是一个简单的有序链表的实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
int id;
char name[20]; float score;
struct student *next;
} Student;
Student *create_student(int id, char *name, float score) {
Student *s = (Student *)malloc(sizeof(Student));
s->id = id;
strcpy(s->name, name);
s->score = score;
s->next = NULL;
return s;
}
void insert_student(Student **head, Student *s) {
Student *p = *head;
Student *prev = NULL;
while (p != NULL && p->id < s->id) {
prev = p;
p = p->next;
}
if (prev == NULL) {
s->next = *head;
*head = s;
} else {
s->next = prev->next;
prev->next = s;
}
}
void print_student(Student *s) {
printf("id: %d, name: %s, score: %.2f\n", s->id, s->name, s->score);
}
void print_student_list(Student *head) {
Student *p = head;
while (p != NULL) {
print_student(p);
p = p->next;
}
}
int main() {
Student *head = NULL;
insert_student(&head, create_student(1001, "Tom", 89.5));
insert_student(&head, create_student(1003, "Jerry", 78.5));
insert_student(&head, create_student(1002, "Alice", 92.0));
print_student_list(head);
return 0;
}
```
上面的代码实现了一个简单的有序链表,其中每个节点表示一个学生,包含学号、姓名和成绩三个属性。通过insert_student函数可以将一个新的学生节点插入到链表中,并保证链表按照学号从小到大排序。print_student_list函数可以遍历整个链表并输出每个学生的信息。
阅读全文