#define TRUE 1
#define FALSE 0
#define OK 1
#ifndef ERROR
#define ERROR -1
#endif
#define OVERFLOW -2
typedef char ElemType;
typedef char status;
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
typedef struct _SqSTACK{
ElemType *base;
ElemType *top;
int stackSize;
}SqSTACK;
/************************************************************************/
//
/************************************************************************/
status StackInit(SqSTACK* stack)
{
stack->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(int));
if(!stack->base)
{
return ERROR;
}
stack->top = stack->base;
stack->stackSize = STACK_INIT_SIZE;
return OK;
}
status Push(SqSTACK* stack,ElemType e)
{
if(stack->top - stack->base >= stack->stackSize)
{
ElemType* preBase = stack->base;
printf("over flow! top = %p memory reallocing ...\n",stack->top);
stack->base = (ElemType*)realloc(stack->base,(stack->stackSize + STACK_INCREMENT)*sizeof(ElemType));
if(!stack->base)
{
perror("realloc");
return ERROR;
}
stack->stackSize += STACK_INCREMENT;
stack->top = stack->base + (stack->top - preBase);
}
*(stack->top) = e;
stack->top +=1;
return OK;
}
ElemType Pop(SqSTACK* stack)
{
ElemType pop;
if(stack->base == stack->top)
{
printf("error:stack empty!\n");
return (ERROR);
}
pop = * -- stack->top;
return pop;
}
status StackDestroy(SqSTACK* stack)
{
free(stack->base);
return 0;
}
status StackIsEmpty(SqSTACK* stack)
{
if(stack->base == stack->top)
{
return TRUE;
}
return FALSE;
}
ElemType GetTop(SqSTACK* stack)
{
if(stack->top == stack->base)
{
return ERROR;
}
return *(stack->top-1);
}