学生管理系统c语言链表
时间: 2023-07-25 15:48:24 浏览: 58
好的,学生管理系统可以使用链表来进行实现。首先,需要定义一个结构体来存储学生的信息,例如:
```
typedef struct student {
char name[20]; // 学生姓名
int age; // 学生年龄
char gender[10]; // 学生性别
char id[20]; // 学生学号
struct student* next; // 指向下一个学生的指针
} Student;
```
然后,可以定义一个链表的头指针,用来指向链表的第一个节点:
```
Student* head = NULL;
```
可以编写一些函数来实现对链表的操作,例如插入节点、删除节点、遍历链表等等。下面是一个简单的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char name[20];
int age;
char gender[10];
char id[20];
struct student* next;
} Student;
Student* head = NULL;
void insert_student() {
Student* new_student = (Student*)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生年龄:");
scanf("%d", &new_student->age);
printf("请输入学生性别:");
scanf("%s", new_student->gender);
printf("请输入学生学号:");
scanf("%s", new_student->id);
new_student->next = NULL;
if (head == NULL) {
head = new_student;
} else {
Student* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_student;
}
printf("插入成功!\n");
}
void delete_student() {
char id[20];
printf("请输入要删除的学生学号:");
scanf("%s", id);
if (head == NULL) {
printf("链表为空!\n");
return;
}
Student* p = head;
Student* prev = NULL;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
if (prev == NULL) {
head = p->next;
} else {
prev->next = p->next;
}
free(p);
printf("删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到该学生!\n");
}
void display_students() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
Student* p = head;
while (p != NULL) {
printf("姓名:%s,年龄:%d,性别:%s,学号:%s\n", p->name, p->age, p->gender, p->id);
p = p->next;
}
}
int main() {
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 插入学生\n");
printf("2. 删除学生\n");
printf("3. 显示学生\n");
printf("4. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
insert_student();
break;
case 2:
delete_student();
break;
case 3:
display_students();
break;
case 4:
return 0;
default:
printf("输入有误,请重新输入!\n");
}
}
}
```
这个示例代码实现了学生信息的插入、删除和显示功能,可以根据需要进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)