#include<stdio.h>
#include<sys/malloc.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
#define STACK_INT_SIZE 5
#define STACKINCREMENT 5
typedef int ElemType;
typedef struct{
ElemType *base;
ElemType top;
int stacksize;
}SqStack;
int InitStack(SqStack *S);
int push(SqStack *S,ElemType *e);
int pop(SqStack S,ElemType *e);
int CreateStack(SqStack *S);
void PrintStack(SqStack *S);
int InitStack(SqStack *S){
S->base=(ElemType *)malloc(STACK_INT_SIZE*sizeof(ElemType));
if (!S->base) {
return ERROR;
}
S->top=-1;
S->stacksize=STACK_INT_SIZE;
return OK;
}
int Push(SqStack *S,ElemType e){
if(S->top==STACK_INT_SIZE-1){
S->base=(ElemType *)realloc(S->base, STACKINCREMENT*sizeof(ElemType));
S->stacksize=S->stacksize+STACKINCREMENT;
}
S->top++;
*(S->base+S->top)=e;
return 1;
}
int Pop(SqStack *S,ElemType *e){
if(S->top==-1) return 0;
else{
*e=*(S->base+S->top);
S->top--;
return 1;
}
}
int CreateStack(SqStack *S){
int e;
if(InitStack(S))
printf("Init Success!\n");
else{
printf("Init Fall!\n");
return ERROR;
}
printf("input data:(Terminated by inputing a character)\n");
while (scanf("%d",&e))
Push(S, e);
return OK;
}
void PrintStack(SqStack *S){
ElemType e;
while (Pop(S, &e)) {
printf("%3d",e);
}
}
int main() {
SqStack SS;
printf("\n1-createStack\n");
CreateStack(&SS);
printf("\n2-createStack\n");
PrintStack(&SS);
printf("\n");
return 0;
}