用c语言写一个学生成绩管理系统,用链表
时间: 2024-05-12 18:17:12 浏览: 102
来存储学生成绩信息,要求能够实现添加学生信息、删除学生信息、修改学生成绩、查询学生成绩等功能。
好的,这是一个比较基本的数据结构应用题,使用链表存储学生成绩信息是比较好的选择。以下是一个实现该功能的C语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct student_t {
char name[MAX_NAME_LEN];
int score;
struct student_t *next;
} Student;
Student *create_student(char *name, int score)
{
Student *s = (Student *)malloc(sizeof(Student));
if (s != NULL) {
strcpy(s->name, name);
s->score = score;
s->next = NULL;
}
return s;
}
Student *find_student(Student *head, char *name)
{
Student *s = head;
while (s != NULL) {
if (strcmp(s->name, name) == 0) {
return s;
}
s = s->next;
}
return NULL;
}
int add_student(Student **head, char *name, int score)
{
Student *new_student = create_student(name, score);
if (new_student == NULL) {
return -1;
}
new_student->next = *head;
*head = new_student;
return 0;
}
int delete_student(Student **head, char *name)
{
Student *prev = NULL;
Student *curr = *head;
while (curr != NULL) {
if (strcmp(curr->name, name) == 0) {
if (prev != NULL) {
prev->next = curr->next;
} else {
*head = curr->next;
}
free(curr);
return 0;
}
prev = curr;
curr = curr->next;
}
return -1;
}
int modify_student_score(Student *head, char *name, int new_score)
{
Student *student = find_student(head, name);
if (student != NULL) {
student->score = new_score;
return 0;
}
return -1;
}
void print_student_info(Student *s)
{
printf("%s\t%d\n", s->name, s->score);
}
void print_all_students(Student *head)
{
Student *s = head;
while (s != NULL) {
print_student_info(s);
s = s->next;
}
}
void free_all_students(Student *head)
{
Student *s = head;
while (s != NULL) {
Student *tmp = s->next;
free(s);
s = tmp;
}
}
int main()
{
Student *head = NULL;
add_student(&head, "Alice", 90);
add_student(&head, "Bob", 85);
add_student(&head, "Charlie", 92);
print_all_students(head);
modify_student_score(head, "Bob", 95);
delete_student(&head, "Charlie");
print_all_students(head);
free_all_students(head);
return 0;
}
```
这是一个基本的链表实现。你可以根据需求进行扩展,比如增加文件或数据库读写功能,或者实现更复杂的排序等操作。
阅读全文