C ++ implements the implementation of basic stack operations

Source: Internet
Author: User

// Base. h

# Include <stdio. h>

# Include <stdlib. h>

# Include <string. h>

# Define true 1

# Define false 0

# Define OK 1

# Define error 0

# Define overflow-2

Typedef int status;

 

// Stack. h

# Include "base. cpp"

# 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 as the new top element of the stack.
 
If (S. top-S.base> = S. stacksize) {// stack full, append 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, use e to return 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

 

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.