Skip to content

Latest commit

 

History

History
90 lines (77 loc) · 2.26 KB

stack.md

File metadata and controls

90 lines (77 loc) · 2.26 KB

<stack>

View contents
  1. push
  2. pop
  3. top
  4. swize
  5. swap
  6. empty
  7. emplace

push

Description : push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1.

Example:

    // Empty stack 
    stack<int> mystack; 
    //pushing elements using push()
    mystack.push(0); 
    mystack.push(1); 
    mystack.push(2); 
  
    while (!mystack.empty()) { 
        //deleting elements using pop()
        cout << ' ' << mystack.top(); 
        mystack.pop(); 
    } 

pop

Description : pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1.

Example:

    // Empty stack 
    stack<int> mystack; 
    //pushing elements using push()
    mystack.push(0); 
    mystack.push(1); 
    mystack.push(2); 
  
    while (!mystack.empty()) { 
        //deleting elements using pop()
        cout << ' ' << mystack.top(); 
        mystack.pop(); 
    } 

empty

Description : empty() function is used to check if the stack container is empty or not.

Example:

    // Empty stack 
    stack<int> mystack; 
    //pushing elements using push()
    mystack.push(0); 
    mystack.push(1); 
    mystack.push(2); 
  
    while (!mystack.empty()) { 
        //deleting elements using pop()
        cout << ' ' << mystack.top(); 
        mystack.pop(); 
    } 

top

Description : top() function is used to reference the top(or the newest) element of the stack.

Example:

    // Empty stack 
    stack<int> mystack; 
    //pushing elements using push()
    mystack.push(0); 
    mystack.push(1); 
    mystack.push(2); 
  
    while (!mystack.empty()) { 
        //deleting elements using pop()
        cout << ' ' << mystack.top(); 
        mystack.pop(); 
    }