1. 定义学生信息的栈数据结构 (包括 学号 姓名 成绩) 2. 设计栈的初始化、入栈、出栈等功能函数,实现学生信息的依次入栈与出栈 3. 编写程序主函数,对各个功能函数进行调用 4. 物理存储结构不限,顺序存储或者链式存储均可
时间: 2023-05-26 14:07:50 浏览: 177
1. 学生信息栈数据结构定义:
```
struct Student {
int id;
char name[20];
float score;
};
struct Stack {
int top;
int size;
struct Student data[MAX_SIZE];
};
```
2. 栈的初始化、入栈、出栈等功能函数的设计:
```
void initStack(struct Stack *stack) {
stack->top = -1;
stack->size = 0;
}
void push(struct Stack *stack, struct Student s) {
if (stack->size == MAX_SIZE) {
printf("Stack is full!");
return;
}
stack->top++;
stack->data[stack->top] = s;
stack->size++;
}
struct Student pop(struct Stack *stack) {
if (stack->size == 0) {
printf("Stack is empty!");
return;
}
struct Student s = stack->data[stack->top];
stack->top--;
stack->size--;
return s;
}
struct Student peek(struct Stack *stack) {
if (stack->size == 0) {
printf("Stack is empty!");
return;
}
return stack->data[stack->top];
}
void displayStack(struct Stack *stack) {
if (stack->size == 0) {
printf("Stack is empty!");
return;
}
printf("[top]\n");
for (int i = stack->top; i >= 0; i--) {
printf("[%d] %s %.2f\n", stack->data[i].id, stack->data[i].name, stack->data[i].score);
}
}
```
3. 程序主函数调用各个功能函数:
```
int main() {
struct Stack stack;
initStack(&stack);
struct Student s1 = { 101, "Tom", 87.5 };
struct Student s2 = { 102, "Jerry", 92.0 };
struct Student s3 = { 103, "Mimi", 78.5 };
push(&stack, s1);
push(&stack, s2);
push(&stack, s3);
displayStack(&stack);
struct Student s = pop(&stack);
printf("Pop student: %d %s %.2f\n", s.id, s.name, s.score);
struct Student s4 = { 104, "Lucy", 90.0 };
push(&stack, s4);
displayStack(&stack);
return 0;
}
```
4. 物理存储结构可使用数组或链表实现。
阅读全文