学生信息管理系统c语言单链表
时间: 2023-08-30 10:09:10 浏览: 76
以下是一个简单的学生信息管理系统的C语言单链表实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char name[20];
int age;
struct student *next;
} Student;
Student *head = NULL;
void add_student() {
Student *new_student = (Student*)malloc(sizeof(Student));
printf("Enter student name: ");
scanf("%s", new_student->name);
printf("Enter student age: ");
scanf("%d", &new_student->age);
new_student->next = NULL;
if (head == NULL) {
head = new_student;
} else {
Student *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_student;
}
printf("Student added successfully!\n");
}
void delete_student() {
if (head == NULL) {
printf("No students to delete!\n");
return;
}
char name[20];
printf("Enter student name to delete: ");
scanf("%s", name);
Student *current = head;
Student *previous = NULL;
while (current != NULL && strcmp(current->name, name) != 0) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return;
}
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
printf("Student deleted successfully!\n");
}
void display_students() {
if (head == NULL) {
printf("No students to display!\n");
return;
}
printf("Name\tAge\n");
printf("----\t---\n");
Student *current = head;
while (current != NULL) {
printf("%s\t%d\n", current->name, current->age);
current = current->next;
}
}
int main() {
int choice = 0;
do {
printf("\nStudent Information Management System\n");
printf("-------------------------------------\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Display students\n");
printf("4. Exit\n\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
display_students();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 4);
return 0;
}
```
该程序使用单链表来存储学生信息,可以添加、删除和显示学生信息。在添加学生时,程序会从用户获取学生的名字和年龄,然后将其添加到链表的末尾。在删除学生时,程序会要求用户输入要删除的学生的名字,然后从链表中删除该学生。在显示学生时,程序会遍历链表并打印每个学生的名字和年龄。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)