#include <stdio.h>
#include <malloc.h>
#define ERROR false
#define OK true
#define MAXSIZE 5
typedef int sElemType;
typedef struct {
sElemType data[MAXSIZE];
int top;
} sqStack;
//压栈
bool Push(sqStack* stack, sElemType e) {
if (stack->top >= MAXSIZE - 1) {
printf("栈以满\n");
return ERROR;
}
stack->top++;
stack->data[stack->top] = e;
return OK;
}
//出栈
bool Pop(sqStack* stack, sElemType* e) {
if (stack->top == -1) {
printf("空栈\n");
return ERROR;
}
*e = stack->data[stack->top];
stack->top--;
return OK;
}
int main() {
sqStack* stack = (sqStack*) malloc(sizeof(sqStack));
stack->top = -1; //初始化栈顶指针
for (int i = 0; i < 5; i++) {
Push(stack, i);
}
sElemType e = 0;
for (int i = 0; i < 5; i++) {
if (Pop(stack, &e))
printf("%d\n", e);
}
return 0;
}