#ifndefPROGRAM1_预定义_H#definePROGRAM1_预定义_H#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2// Status 是函数的类型, 其值是函数结果状态代码typedefint Status;#endif//PROGRAM1_预定义_H
Stack.h
#pragmaonce#include"预定义.h"#include<stdio.h>#include<stdlib.h>typedefint Status;#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstruct{int x, y;//行列坐标int d;//上一点来此的方向} datatype;typedef datatype SElemType;typedefstruct{
datatype* base,* top;int stacksize;} SqStack;
SqStack S;
Status InitStack(SqStack &S);
Status DestroyStack(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);voidPrintStack(SqStack S);
Status InitStack(SqStack &S){
S.base =(datatype*)malloc(STACK_INIT_SIZE *sizeof(datatype));if(!S.base)exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;return OK;}
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)exit(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 StackEmpty(SqStack S){if(S.top == S.base)return TRUE;return FALSE;}
Status DestroyStack(SqStack &S){if(!S.base)return ERROR;free(S.base);
S.base = S.top =NULL;
S.stacksize =0;return OK;}
Status ClearStack(SqStack &S){if(!S.base)return ERROR;
SElemType e;while(!StackEmpty(S)){GetTop(S, e);printf_s("%d", e);}// S.top = S.base;return OK;}voidPrintStack(SqStack S){printf_s("迷宫走法之一:\n");
SElemType* p = S.base,* q = S.top;while(p != q){printf_s("<%d %d>\n", p->x, p->y);
p++;}}