基于C语言利用线性表的基本操作实现对一个班级学生信息(包括学号、姓名、学院、专业、班级、性别、年龄)的管理系统
时间: 2023-10-10 13:09:45 浏览: 352
学生信息管理系统, 实现学生信息包括:学号、姓名、性别、年龄、班级等信息
5星 · 资源好评率100%
以下是一个基于C语言利用线性表的学生信息管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_COLLEGE_LEN 30
#define MAX_MAJOR_LEN 30
#define MAX_CLASS_LEN 10
/* 定义学生信息结构体 */
typedef struct student {
int id; // 学号
char name[MAX_NAME_LEN]; // 姓名
char college[MAX_COLLEGE_LEN]; // 学院
char major[MAX_MAJOR_LEN]; // 专业
char class[MAX_CLASS_LEN]; // 班级
char gender; // 性别
int age; // 年龄
} Student;
/* 定义节点结构体 */
typedef struct node {
Student data; // 数据域
struct node *next; // 指针域
} Node;
/* 定义链表结构体 */
typedef struct linked_list {
Node *head; // 头节点指针
int len; // 链表长度
} LinkedList;
/* 初始化链表 */
void init_list(LinkedList *list) {
list->head = NULL;
list->len = 0;
}
/* 创建一个节点 */
Node *create_node(Student data) {
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL) {
printf("Error: out of memory.\n");
exit(1);
}
node->data = data;
node->next = NULL;
return node;
}
/* 在链表末尾插入一个节点 */
void append_node(LinkedList *list, Node *node) {
if (list->head == NULL) {
list->head = node;
} else {
Node *p = list->head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
list->len++;
}
/* 在链表中查找指定学号的节点 */
Node *find_node_by_id(LinkedList *list, int id) {
Node *p = list->head;
while (p != NULL) {
if (p->data.id == id) {
return p;
}
p = p->next;
}
return NULL;
}
/* 在链表中删除指定学号的节点 */
void delete_node_by_id(LinkedList *list, int id) {
Node *p = list->head;
Node *prev = NULL;
while (p != NULL) {
if (p->data.id == id) {
if (prev == NULL) {
list->head = p->next;
} else {
prev->next = p->next;
}
free(p);
list->len--;
return;
}
prev = p;
p = p->next;
}
}
/* 打印链表中的所有节点 */
void print_list(LinkedList *list) {
Node *p = list->head;
printf("ID\tName\tCollege\tMajor\tClass\tGender\tAge\n");
while (p != NULL) {
printf("%d\t%s\t%s\t%s\t%s\t%c\t%d\n",
p->data.id,
p->data.name,
p->data.college,
p->data.major,
p->data.class,
p->data.gender,
p->data.age);
p = p->next;
}
}
/* 从标准输入中读取一个学生信息 */
Student read_student_info() {
Student s;
printf("Please enter the student's ID, name, college, major, class, gender, and age, separated by spaces:\n");
scanf("%d %s %s %s %s %c %d",
&s.id,
s.name,
s.college,
s.major,
s.class,
&s.gender,
&s.age);
return s;
}
/* 主函数 */
int main() {
LinkedList list;
init_list(&list);
int choice;
while (1) {
printf("Please choose an operation:\n");
printf("1. Add a student\n");
printf("2. Delete a student\n");
printf("3. Search for a student\n");
printf("4. Print all students\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1: {
Student s = read_student_info();
Node *node = create_node(s);
append_node(&list, node);
printf("Added student:\n");
printf("ID\tName\tCollege\tMajor\tClass\tGender\tAge\n");
printf("%d\t%s\t%s\t%s\t%s\t%c\t%d\n",
s.id,
s.name,
s.college,
s.major,
s.class,
s.gender,
s.age);
break;
}
case 2: {
int id;
printf("Please enter the student's ID you want to delete:\n");
scanf("%d", &id);
delete_node_by_id(&list, id);
printf("Deleted student with ID %d.\n", id);
break;
}
case 3: {
int id;
printf("Please enter the student's ID you want to search for:\n");
scanf("%d", &id);
Node *node = find_node_by_id(&list, id);
if (node != NULL) {
printf("Found student:\n");
printf("ID\tName\tCollege\tMajor\tClass\tGender\tAge\n");
printf("%d\t%s\t%s\t%s\t%s\t%c\t%d\n",
node->data.id,
node->data.name,
node->data.college,
node->data.major,
node->data.class,
node->data.gender,
node->data.age);
} else {
printf("Student with ID %d not found.\n", id);
}
break;
}
case 4: {
print_list(&list);
break;
}
case 5: {
printf("Goodbye!\n");
exit(0);
}
default: {
printf("Invalid choice.\n");
break;
}
}
}
return 0;
}
```
这个程序使用了链表来存储学生信息,可以进行添加、删除、查找和打印所有学生信息的操作。在主函数中,通过一个循环来不断接收用户的选择并执行相应的操作。每次添加或删除学生信息后,都会打印出相应的信息以供用户确认。打印所有学生信息的操作会输出一个表格,包含所有学生的信息。
阅读全文