#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW 0
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStask;
Status InitStack(SqStask *S);
Status Pop(SqStask *S,SElemType *e);
Status Push(SqStask *S,SElemType e);
Status GetTop(SqStask S,SElemType *e);
int main(int argc, const char * argv[]) {
SqStask S;
int e;
InitStack(&S);
Push(&S, 5);
Pop(&S, &e);
printf("%d",e);
return 0;
}
Status InitStack(SqStask *S){
(*S).base=(SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
Status Pop(SqStask *S,SElemType *e){
if((*S).top==(*S).base)
return ERROR;
*e=*(--(*S).top);
return OK;
}
Status Push(SqStask *S,SElemType e){
int *newbase;
if((*S).top-(*S).base>=(*S).stacksize){
newbase=((SElemType *)realloc((*S).base, ((*S).stacksize+STACKINCREMENT)*sizeof(SElemType)));
if (!newbase) {
exit(OVERFLOW);
}
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCREMENT;}
*((*S).top++)=e;
return OK;
}
Status GetTop(SqStask S,SElemType *e){
if (S.top==S.base) {
return ERROR;
}
*e=*(S.top-1);
return OK;
}