在右侧编辑器中的Begin-End之间补充代码,设计一个以链表为基础的学生信息管理,系统中包含五个函数的实现,具体功能如下:
时间: 2024-05-03 12:20:12 浏览: 86
学生 信息 管理 用链表实现
好的,我明白了你的要求。以下是我为你设计的学生信息管理系统:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN 20
#define ID_LEN 10
typedef struct student {
char id[ID_LEN];
char name[NAME_LEN];
int score;
struct student *next;
} student;
student *create_list();
void print_list(student *head);
void add_student(student *head);
void delete_student(student *head);
void search_student(student *head);
int main() {
student *head = create_list();
int choice;
while (1) {
printf("\n1. Add student\n");
printf("2. Delete student\n");
printf("3. Search student\n");
printf("4. Print list\n");
printf("5. Quit\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(head);
break;
case 2:
delete_student(head);
break;
case 3:
search_student(head);
break;
case 4:
print_list(head);
break;
case 5:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
student *create_list() {
student *head = (student *)malloc(sizeof(student));
head->next = NULL;
return head;
}
void print_list(student *head) {
student *p = head->next;
printf("ID\tName\tScore\n");
while (p != NULL) {
printf("%s\t%s\t%d\n", p->id, p->name, p->score);
p = p->next;
}
}
void add_student(student *head) {
student *p = head;
while (p->next != NULL) {
p = p->next;
}
student *new_student = (student *)malloc(sizeof(student));
printf("Enter student ID: ");
scanf("%s", new_student->id);
printf("Enter student name: ");
scanf("%s", new_student->name);
printf("Enter student score: ");
scanf("%d", &new_student->score);
new_student->next = NULL;
p->next = new_student;
}
void delete_student(student *head) {
char id[ID_LEN];
printf("Enter student ID to delete: ");
scanf("%s", id);
student *p = head->next;
student *prev = head;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
prev->next = p->next;
printf("Deleted student with ID %s\n", p->id);
free(p);
return;
}
prev = p;
p = p->next;
}
printf("Student with ID %s not found.\n", id);
}
void search_student(student *head) {
char id[ID_LEN];
printf("Enter student ID to search: ");
scanf("%s", id);
student *p = head->next;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
printf("Found student:\n");
printf("ID:\t%s\nName:\t%s\nScore:\t%d\n", p->id, p->name, p->score);
return;
}
p = p->next;
}
printf("Student with ID %s not found.\n", id);
}
```
这个程序实现了以下五个函数:
1. `create_list()`:创建一个链表,并返回头结点。头结点不存储有效数据,因此它的成员变量`next`初始化为`NULL`。
2. `print_list(student *head)`:遍历链表并打印出所有学生信息,包括ID、姓名和成绩。
3. `add_student(student *head)`:向链表末尾添加一个学生节点,并要求用户输入该学生的ID、姓名和成绩。
4. `delete_student(student *head)`:根据用户输入的学生ID删除链表中对应的学生节点。
5. `search_student(student *head)`:根据用户输入的学生ID查找链表中对应的学生节点,并打印出该学生的ID、姓名和成绩。
你可以根据这个程序的框架,在Begin-End之间进一步编写代码,实现一个基于链表的学生信息管理系统。
阅读全文