用数据结构c++版,利用插入,删除,查找,任意编写学生信息表,表中包含学生姓名,学号,成绩
时间: 2024-04-30 07:22:33 浏览: 16
以下是用C语言实现学生信息表的代码,包括插入、删除、查找等操作:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 学生信息结构体
typedef struct {
char name[20]; // 姓名
int id; // 学号
int score; // 成绩
} student;
// 学生信息表结构体
typedef struct {
student *data; // 数据
int size; // 当前大小
int capacity; // 最大容量
} student_table;
// 初始化学生信息表
void init_table(student_table *table, int capacity) {
table->data = (student*)malloc(sizeof(student) * capacity);
table->size = 0;
table->capacity = capacity;
}
// 插入学生信息
void insert(student_table *table, student s) {
if (table->size == table->capacity) {
printf("Table is full.\n");
return;
}
table->data[table->size++] = s;
}
// 删除学生信息
void delete(student_table *table, int id) {
int i;
for (i = 0; i < table->size; i++) {
if (table->data[i].id == id) {
break;
}
}
if (i == table->size) {
printf("Student not found.\n");
return;
}
for (; i < table->size - 1; i++) {
table->data[i] = table->data[i + 1];
}
table->size--;
}
// 查找学生信息
student* search(student_table *table, char* name) {
int i;
for (i = 0; i < table->size; i++) {
if (strcmp(table->data[i].name, name) == 0) {
return &table->data[i];
}
}
return NULL;
}
int main() {
student_table table;
init_table(&table, 10);
// 添加学生信息
insert(&table, (student){"Tom", 1001, 90});
insert(&table, (student){"Jerry", 1002, 85});
insert(&table, (student){"Bob", 1003, 92});
// 查找学生信息
student *s1 = search(&table, "Tom");
if (s1 != NULL) {
printf("%s's score is %d.\n", s1->name, s1->score);
} else {
printf("Tom not found.\n");
}
// 删除学生信息
delete(&table, 1002);
// 查找学生信息
student *s2 = search(&table, "Jerry");
if (s2 != NULL) {
printf("%s's score is %d.\n", s2->name, s2->score);
} else {
printf("Jerry not found.\n");
}
return 0;
}
```
使用该代码,可以创建一个学生信息表,包含学生姓名、学号和成绩,并可以进行插入、删除、查找等操作。可以根据实际需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)