C语言编写数据结果线性表学生表
时间: 2024-10-01 17:08:04 浏览: 46
在C语言中,我们可以创建一个结构体来表示学生表的数据,然后使用数组或链表的形式来存储一组学生的信息。这里是一个简单的例子,假设我们有一个学生结构体`Student`,包含姓名`name`、学号`id`和成绩`score`:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 10
typedef struct {
char name[50];
int id;
float score;
} Student;
// 动态内存分配的函数用于创建学生表
Student* createStudentTable() {
Student *students = (Student*)malloc(MAX_STUDENTS * sizeof(Student));
if (students == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
return students;
}
// 添加学生到学生表的函数
void addStudent(Student *table, int index, const char *name, int id, float score) {
table[index].name = name;
table[index].id = id;
table[index].score = score;
}
// 显示学生表内容的函数
void displayStudentTable(Student *table) {
for (int i = 0; i < MAX_STUDENTS && table[i].id != -1; i++) { // 简单示例,实际可能需要处理表满情况
printf("ID: %d, Name: %s, Score: %.2f\n", table[i].id, table[i].name, table[i].score);
}
}
int main() {
Student *studentTable = createStudentTable();
if (studentTable != NULL) {
// 添加一些学生数据
addStudent(studentTable, 0, "张三", 1001, 90.0);
addStudent(studentTable, 1, "李四", 1002, 85.5);
// ... 更多添加操作 ...
// 展示学生表
displayStudentTable(studentTable);
// 清理内存
free(studentTable);
} else {
return 1;
}
return 0;
}
```
阅读全文
相关推荐


















