代码:
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 100
#define STACKINCREMENT 20
#define OK 1
#define ERROR -1
typedef int Status;
typedef int ElemType;
// 栈的动态顺序存储表示
typedef struct sqstack{
ElemType *bottom; /*栈不存在时值为NULL*/
ElemType *top; /*栈顶指针*/
int stacksize; /*当前已分配空间, 以元素为单位*/
}SqStack;
//栈的初始化, 构建一个空栈
Status Init_Stack(SqStack *S){
// SqStack S;
S->bottom = (ElemType *)malloc(STACK_SIZE * sizeof(ElemType));
if(!S->bottom) return ERROR;
S->top = S->bottom; /*栈空时, 栈顶和栈底指针相同*/
S->stacksize = STACK_SIZE;
return OK;
}
//压栈
Status push(SqStack *S, ElemType *e){
// int STACKINCREMENT = 20;
if(S->top - S->bottom >= S->stacksize - 1){
S->bottom = (ElemType *)realloc(S->bottom, (STACKINCREMENT + STACK_SIZE) * sizeof(ElemType));/*栈满 ,追加存储空间*/
if(!S