#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S){ //构造栈
S.base=(int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S.base){
printf("初始化失败\n");
exit;
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}
void GetTop(SqStack S,int &e){ //取栈顶元素
if(S.base==S.top)//栈为空的条件
printf("栈为空\n");
e=*(S.top-1);
}
void push(SqStack &S,int e){ //进栈
if(S.top-S.base>=S.stacksize){
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
if(!S.base){
printf("初始化失败\n");
exit;
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
printf("%d\n",S.top);
*S.top=e;
S.top++;
printf("%d\n",S.top);
}
void pop(SqStack &S,int &e){
if(S.base==S.top)
printf("栈为空\n");
printf("%d\n",S.top);
e=*(--S.top);
}
void main(){
SqStack S;
InitStack(S);
int e=0;
push(S,2);
pop(S,e);
printf("%d\n",e);
}