顺序栈的一些基本操作,包括进栈和出栈
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct {
int *base;
int *top;
int stacksize;
}SqStack;
SqStack InitStack(SqStack S){
S.base = (int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)exit(1);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return S;
}
int GetTop(SqStack S){
if (S.base == S.top)return 0;
int e = *(S.top-1);
return e ;
}
SqStack 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)exit(1);
}
*(S.top) = e;
S.top = S.top+1;
return S;
}
SqStack Pop(SqStack S){
if(S.top == S.base)return S;
S.top = S.top-1;
return S;
}
int main(void){
SqStack S;
S = InitStack(S);
S = Push(S,123);
S = Push(S,456);
int top = GetTop(S);
printf("%d\n",top);
S = Pop(S);
int top1 = GetTop(S);
printf("%d\n",top1);
system("PAUSE");
return 0 ;
}