Stack sequential storage implementation

Source: Internet
Author: User

Stack sequential storage implementation

# Include
 
  
# Include
  
   
Using namespace std; # define STACK_SIZE 100 struct Stack {int * top; int * base; int Size;} Stack; /********************** Function Statements ************** * ******/bool InitStack (struct Stack & S ); // build the Stack bool IsEmpty (struct Stack & S); // judge the empty Stack bool IsFull (struct Stack & S); // judge whether the Stack is full void Push (struct Stack & S, int e); // press the Stack and insert the data void Pop (struct Stack & S, int & e); // Delete the data, that is, the Pop-up data bool DestroyStack (struct Stack & S ); // destroy Stack bool ClearStack (struct Stack & S); // clear Stack void PrintStack (struct Stack & S); // print the elements in the Stack, that is, output/********************main ************* * **********/int main () {struct Stack S; int B, n; if (InitStack (S) = false) {cout <"Init Stack error! "<Endl; exit (0) ;}cout <" push a data into stack: "<endl; cin> n; // cout <"Stack operation" <endl; while (n --) {Push (S, n); PrintStack (S );} // cout <"Stack operation" <endl; while (n <4) {Pop (S, B); PrintStack (S); n ++ ;} // clear stack cout <"Clear stack operation" <endl; if (ClearStack (S) PrintStack (S ); // destroy stack cout <"Destroy stack operation" <endl; DestroyStack (S); cout <"S. base = "<S. base <";" <"S. top = "<S. top <";" <" S. size = "<S. size <endl; return 0 ;} /************************************ print the elements in the stack, top-down display * that is, output from the end of the stack to the beginning of the stack *************************** * ***/void PrintStack (struct Stack & S) {int n = S. top-S. base; int index; // if the stack is empty, if (! N) {cout <".................................. "<endl; cout <" Stack is Empty... "<
   
    
= B, it indicates Stack Overflow ************************************ * *****/bool IsFull (struct Stack & S) {int A = S. top-S. base; int B = S. size; if (A >= B) {cout <"Stack is Full" <endl; return true;} else return false ;} /*************************************** * ******************** Push * stack operation * the stack is a special linear table, it does not support random access, so it can only be stored in order. * For example, the bullet in the bullet clip (this example is different from the implementation in this example: the top pointer of the stack remains unchanged, and the bottom pointer of the stack is swimming. * ++ In this program, the bottom pointer of the stack remains unchanged, and the top pointer of the stack is swimming .) **************************************** * *******************/Void Push (struct Stack & S, int e) {if (! IsFull (S) {* (S. top) = e; S. top ++ ;}} /*************************************** * *************************** Pop * stack operation * is a special linear table, it does not support random access, so it can only be stored in order. * For example, a bullet is introduced in the bullet clip (this example is different from the implementation in this example: the top pointer of the stack remains unchanged, and the bottom pointer of the stack is swimming. * ++ In this program, the bottom pointer of the stack remains unchanged, and the top pointer of the stack is swimming .) **************************************** * *************************/Void Pop (struct Stack & S, int & e) {if (IsFull (S) {cout <"Pop error" <endl;} else {e = * (S. top-1); S. top --;}}
   
  
 


Click Open Link


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.