- #include<malloc.h>
- #include<string.h>
- #include<stdio.h>
- #define OVERFLOW -1
- #define STACK_INIT_SIZE 10
- #define STACKINCREMENT 2
- #define OK 1
- #define ERROR 0
- #define TRUE 1
- #define FALSE 0
- typedef int SElemType;
- typedef int Status;
- struct SqStack
- {
- SElemType *base;
- SElemType *top;
- int stacksize;
- };
- Status InitStack(SqStack &S);
- Status DestoryStack(SqStack &S);
- Status ClearStack(SqStack &S);
- Status StackEmpty(SqStack S);
- int StackLength(SqStack S);
- Status GetTop(SqStack S, SElemType &e);
- Status Push(SqStack &S, SElemType e);
- Status Pop(SqStack &S, SElemType &e);
- Status StackTraverse(SqStack S, Status(*visit)());
- Status InitStack(SqStack &S)
- {
- S.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
- if(!S.base) return OVERFLOW;
- S.top = S.base;
- S.stacksize = STACK_INIT_SIZE;
- return OK;
- }
- Status DestoryStack(SqStack &S)
- {
- free(S.base);
- S.base = NULL;
- S.top = NULL;
- S.stacksize = 0;
- return OK;
- }
- Status ClearStack(SqStack &S)
- {
- S.top = S.base;
- return OK;
- }
- Status StackEmpty(SqStack S)
- {
- if(S.top == S.base)
- return TRUE;
- else
- return FALSE;
- }
- int StackLength(SqStack S)
- {
- return S.top - S.base;
- }
- Status GetTop(SqStack S,SElemType &e)
- {
- if(S.top == S.base) return ERROR;
- e = *(S.top -1);
- return OK;
- }
- Status Push(SqStack &S, SElemType e)
- {
- if(S.top - S.base >= S.stacksize)
- {
- S.base = (SElemType *)realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(SElemType));
- if(!S.base) return OVERFLOW;
- S.top = S.base + S.stacksize;
- S.stacksize += STACKINCREMENT;
- }
- *S.top++ = e;
- return OK;
- }
- Status Pop(SqStack &S, SElemType &e)
- {
- if(S.top == S.base) return ERROR;
- e = * --S.top;
- return OK;
- }
- Status StackTraverse(SqStack S, Status(* visit)(SElemType))
- {
- while(S.top > S.base)
- visit(*S.base++);
- printf("\n");
- return OK;
- }
- Status visit(SElemType e)
- {
- printf("%d ", e);
- return OK;
- }
- int main(){
- .....
- }