Use stacks to solve maze Problems

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.