#define _CRT_SECURE_NO_WARNINGS // Exp3-2_SeqStack_Expandable #include <stdio.h> #include <stdlib.h> #include <string.h> #define STACK_INIT_LENGTH 4 #define STACK_INCREMENT 3 #define STU_ID_MAXLEN 16 #define STU_NAME_MAXLEN 20 // 学生结构体 typedef struct { char stuId[STU_ID_MAXLEN]; char name[STU_NAME_MAXLEN]; char gender; float score; } Stu; // 动态顺序栈结构体 typedef struct { Stu *base; // 存放学生的动态数组的起始地址,数组大小可以扩充,因此称其为“动态” int stackSize; // 当前顺序栈中数组单元的个数 int top; // 栈顶元素的下标 } SeqStack; Stu *ps; int stuNum; // Get alternative all students' data void loadAllStudent() { FILE *fp; int i = 0; if ((fp = fopen("allStu.dat", "rb")) == NULL) { printf("Cannot open the data file, failed to save the student information!"); return; } // 开始读文件,先读人数,根据人数开辟空间的大小 fread(&stuNum, sizeof(int), 1, fp); ps = (Stu *)malloc(sizeof(Stu)*stuNum); while (i < stuNum) { fread(&(ps[i]), sizeof(Stu), 1, fp); i++; } // fread(ps, sizeof(Stu), stuNum, fp); fclose(fp); } void menu() { printf("\n\t****Expandable Sequence Stack(Space can be expanded dynamically)****\n"); printf("\t* 1 Choose a student & Push *\n"); printf("\t* 2 Pop a student from the stack *\n"); printf("\t* 3 Read the top element of the stack and print *\n"); printf("\t* 4 Show all the students in the stack *\n"); printf("\t* 0 Save the data and Exit the program *\n"); printf("\t********************************************************************\n"); printf("\tPlease select a menu item:\n"); } /* Check whether the file stu3-2.dat exist in the current directory. If it exists, the data will be read from it to construct a sequence stack. */ void init(SeqStack &seqStack) { FILE *fp; Stu stmp; int
时间: 2025-05-15 16:52:53 浏览: 17
### 可扩展顺序栈的初始化
在 C 语言中,可以通过动态内存分配技术实现可扩展顺序栈。这种栈可以在需要时自动调整其容量大小。以下是其实现方式:
#### 动态扩容机制
当栈满时,通过 `realloc` 函数重新分配更大的存储空间给栈的数据区域。通常情况下,可以将当前容量加倍以减少频繁的内存分配操作。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data; // 数据存储区
int capacity; // 当前最大容量
int top; // 栈顶指针
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->capacity = 10; // 初始容量设为10
s->top = -1;
s->data = (int *)malloc(s->capacity * sizeof(int));
}
// 扩展栈容量
void expandCapacity(Stack *s) {
s->capacity *= 2; // 容量翻倍
s->data = (int *)realloc(s->data, s->capacity * sizeof(int)); // 重新分配内存
}
```
上述代码展示了如何创建一个初始容量为10的栈,并提供了扩展容量的功能[^6]。
---
### 文件读取学生数据
为了从文件中加载学生数据,假设每条记录包含学生的学号和成绩,则可以设计如下结构体并完成相应的文件读取逻辑。
#### 结构体定义
```c
typedef struct {
int id; // 学生ID
float score; // 成绩
} Student;
```
#### 文件读取函数
以下是一个完整的例子,展示如何从文件中逐行读取学生信息并存入数组或链表中。
```c
#define MAX_STUDENTS 100
Student students[MAX_STUDENTS];
int studentCount = 0;
void loadStudentsFromFile(const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
printf("无法打开文件 %s\n", filename);
return;
}
while (fscanf(file, "%d%f", &(students[studentCount].id), &(students[studentCount].score)) != EOF && studentCount < MAX_STUDENTS) {
studentCount++;
}
fclose(file);
}
```
此段代码实现了从指定文件名中按格式 `%d %f` 加载学生 ID 和分数的操作[^7]。
如果希望支持更灵活的学生数量处理,还可以改用动态数组代替固定长度数组。
---
### 总结
以上分别介绍了两种功能模块的设计思路:一是基于动态内存管理构建具有自适应能力的顺序栈;二是针对特定需求(如学生数据),提供了一种简单有效的文件解析方案。两者均体现了良好的编程实践原则——清晰、高效且易于维护。
阅读全文
相关推荐

















