用c语言编写:利用线性表的基本操作实现对一个班级学生信息(包括:学号、姓名、学院、专业、班级、性别、年龄等)管理的系统(主要功能包括:数据录入、插入、删除、输出、查找等)
时间: 2024-10-10 16:08:54 浏览: 77
在C语言中,我们可以使用结构体(struct)来定义学生的属性,并创建一个链表来管理这个班级的学生信息。首先,我们定义一个名为`Student`的结构体:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int student_id;
char name[50];
char college[50];
char major[50];
char class_name[50];
char gender[10];
int age;
} Student;
// 定义链表节点
typedef struct Node {
Student info;
struct Node* next;
} Node;
// 链表头指针
Node* head = NULL;
// 管理函数
void add_student(Student* new_student) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode != NULL) {
newNode->info = *new_student;
newNode->next = head;
head = newNode;
} else {
printf("Memory allocation failed.\n");
}
}
void remove_student(int id) {
Node* current = head;
while (current != NULL) {
if (current->info.student_id == id) {
Node* temp = current;
current = current->next;
free(temp);
return;
}
current = current->next;
}
printf("Student with ID %d not found.\n", id);
}
void display_students() {
Node* current = head;
if (head != NULL) {
while (current != NULL) {
printf("ID: %d, Name: %s, College: %s, Major: %s, Class: %s, Gender: %s, Age: %d\n",
current->info.student_id, current->info.name, current->info.college,
current->info.major, current->info.class_name, current->info.gender, current->info.age);
current = current->next;
}
} else {
printf("No students in the list.\n");
}
}
// 其他函数如查找学生可以类似地实现
int main() {
// 初始化示例
Student example = {1, "张三", "计算机科学", "软件工程", "一班", "男", 20};
add_student(&example);
// 使用其他管理函数...
return 0;
}
```
在这个例子中,我们实现了数据录入(通过`add_student`函数),插入(`add_student`)、删除(`remove_student`)、输出(`display_students`)和查找等功能。注意,在实际应用中,还需要处理更多的边界情况和错误检查。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)