使用c语言,使用链式存储结构,实现学生信息管理系统,实现功能增删改查
时间: 2023-08-06 21:01:42 浏览: 88
main_C语言_增删改查_商品房信息管理系统_
好的,以下是使用链式存储结构实现学生信息管理系统的代码(包含增删改查功能):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
int age;
struct student *next;
} Student;
Student *head = NULL; // 学生信息链表头指针
// 函数声明
void add();
void delete();
void modify();
void search();
void print_all();
void clear_all();
int main() {
int op;
while (1) {
printf("\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Print all students\n");
printf("6. Clear all students\n");
printf("0. Exit\n");
printf("Please select an operation: ");
scanf("%d", &op);
switch (op) {
case 1:
add();
break;
case 2:
delete();
break;
case 3:
modify();
break;
case 4:
search();
break;
case 5:
print_all();
break;
case 6:
clear_all();
break;
case 0:
printf("Goodbye!\n");
return 0;
default:
printf("Invalid operation!\n");
break;
}
}
}
// 添加学生信息
void add() {
Student *p = (Student *)malloc(sizeof(Student));
if (p == NULL) {
printf("Memory allocation failed!\n");
return;
}
printf("Please input student name: ");
scanf("%s", p->name);
printf("Please input student ID: ");
scanf("%s", p->id);
printf("Please input student age: ");
scanf("%d", &(p->age));
p->next = head;
head = p;
printf("Student added successfully!\n");
}
// 删除学生信息
void delete() {
char id[MAX_ID_LEN];
printf("Please input the ID of the student to be deleted: ");
scanf("%s", id);
Student *p = head;
Student *pre = NULL;
while (p != NULL && strcmp(p->id, id) != 0) {
pre = p;
p = p->next;
}
if (p == NULL) {
printf("Student with ID %s not found!\n", id);
return;
}
if (pre == NULL) {
head = p->next;
} else {
pre->next = p->next;
}
free(p);
printf("Student with ID %s deleted successfully!\n", id);
}
// 修改学生信息
void modify() {
char id[MAX_ID_LEN];
printf("Please input the ID of the student to be modified: ");
scanf("%s", id);
Student *p = head;
while (p != NULL && strcmp(p->id, id) != 0) {
p = p->next;
}
if (p == NULL) {
printf("Student with ID %s not found!\n", id);
return;
}
printf("Please input new student name (current name is %s): ", p->name);
scanf("%s", p->name);
printf("Please input new student age (current age is %d): ", p->age);
scanf("%d", &(p->age));
printf("Student with ID %s modified successfully!\n", id);
}
// 查找学生信息
void search() {
char id[MAX_ID_LEN];
printf("Please input the ID of the student to be searched: ");
scanf("%s", id);
Student *p = head;
while (p != NULL && strcmp(p->id, id) != 0) {
p = p->next;
}
if (p == NULL) {
printf("Student with ID %s not found!\n", id);
return;
}
printf("Student found:\n");
printf("Name: %s\n", p->name);
printf("ID: %s\n", p->id);
printf("Age: %d\n", p->age);
}
// 打印所有学生信息
void print_all() {
if (head == NULL) {
printf("No students to show!\n");
return;
}
printf("All students:\n");
Student *p = head;
while (p != NULL) {
printf("Name: %s, ID: %s, Age: %d\n", p->name, p->id, p->age);
p = p->next;
}
}
// 清空所有学生信息
void clear_all() {
Student *p = head;
while (p != NULL) {
head = p->next;
free(p);
p = head;
}
printf("All students cleared successfully!\n");
}
```
在上面的代码中,我们使用了一个结构体 `Student` 来表示每个学生的信息,包含姓名、学号、年龄和下一个学生信息的指针。我们使用了一个链表来存储所有学生的信息,链表的头指针为 `head`。
接下来,我们实现了增删改查和打印所有学生信息的功能,具体实现方式在代码注释中有说明。最后,我们还提供了清空所有学生信息的功能,以便在需要时清除所有数据。
阅读全文