Source program:
// Base. h
# Include
# Include
# Include
# Define TRUE 1
# Define FALSE 0
# Define OK 1
# Define ERROR 0
# Define OVERFLOW-2
Typedef int Status;
// Stack. h
# Include "base. h"
# Define INIT_SIZE 100 // initial storage space allocation
# Define INCREMENT 10 // INCREMENT of storage space allocation
Typedef struct {// position of column c in the r column in the maze
Int r;
Int c;
} PostType;
Typedef struct {
Int ord; // the serial number of the current position in the path
PostType seat; // current Coordinate
Int di; // the direction of the next Coordinate
} SElemType; // stack element type
Typedef struct {
SElemType * base; // stack base address, empty after being destroyed before construction
SElemType * top; // stack top
Int stackSize; // stack capacity
} Stack; // Stack type
Status InitStack (Stack & S) {// construct an empty Stack s
S. base = (SElemType *) malloc (INIT_SIZE * sizeof (SElemType ));
If (! S. base)
Exit (OVERFLOW); // storage allocation failed
S. top = S. base;
S. stackSize = INIT_SIZE;
Return OK;
} // InitStack
Status StackEmpty (Stack S ){
// If s is null, TRUE is returned; otherwise, FALSE is returned.
If (S. top = S. base)
Return TRUE;
Return FALSE;
} // StackEmpty
Status Push (Stack & S, SElemType e ){
// Insert element e into the new stack top Element
If (S. top-S.base> = S. stackSize) {// stack full, add space
S. base = (SElemType *) realloc (S. base, (S. stackSize + INCREMENT) * sizeof (SElemType ));
If (! S. base)
Exit (OVERFLOW); // storage allocation failed
S. top = S. base + S. stackSize;
S. stackSize + = INCREMENT;
}
* S. top ++ = e;
Return OK;
} // Push
Status Pop (Stack & S, SElemType & e) {// If the Stack is not empty, delete the Stack // use e to return the result and Return OK; otherwise, an ERROR is returned.
If (S. top = S. base)
Return ERROR;
E = * -- S. top;
Return OK;
} // Pop
Status DestroyStack (Stack & S) {// destroy Stack S,
Free (S. base );
S. top = S. base;
Return OK;
} // DestroyStack