Skip to content
Related Articles

Related Articles

Stack push() and pop() in C++ STL

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 16 Jun, 2022

Stacks are a type of container adaptors that follow LIFO(Last In First Out) property, where a new element is added at one end and an element(at the top) is removed from that end only. Basically, the insertion and deletion happen on the top of the stack itself.

stack::push()

push() function is used to insert or ‘push’ an element at the top of the stack. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <stack> header file. The element is added to the stack container and the size of the stack is increased by 1.
Syntax:

stackname.push(value)

Parameters: The value of the element to be inserted is passed as the parameter.
Result: Adds an element of value the same as that of the parameter passed at the top of the stack.

Examples: 

Input :   mystack
          mystack.push(6);
Output :  6
 
Input :   mystack
          mystack.push(0);
          mystack.push(1);
Output :  0, 1

Errors and Exceptions:

  • Shows an error if the value passed doesn’t match the stack type. 
  • Shows no exception throw guarantee if the parameter doesn’t throw any exception.

CPP




// CPP program to illustrate
// Implementation of push() function
 
#include <iostream>
#include <stack>
using namespace std;
 
int main()
{
    // Empty stack
    stack<int> mystack;
    mystack.push(0);
    mystack.push(1);
    mystack.push(2);
 
    // Printing content of stack
    while (!mystack.empty()) {
        cout << ' ' << mystack.top();
        mystack.pop();
    }
}

Output

 2 1 0

NOTE: Here, output is printed on the basis of LIFO property.

stack::pop()

The pop() function is used to remove or ‘pop’ an element from the top of the stack(newest or the topmost element in the stack). This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <stack> header file. The element is removed from the stack container and the size of the stack is decreased by 1.
Syntax:

stackname.pop()

Parameters: No parameters are passed.

Result: Removes the newest element in the stack or basically the top element.

Examples: 

Input :   mystack = 0, 1, 2
          mystack.pop();
Output :  0, 1
 
Input :   mystack = 0, 1, 2, 3, 4, 5
          mystack.pop();
Output :  0, 1, 2, 3, 4

Errors and Exceptions:

  • Shows error if a parameter is passed. 
  • Shows no exception throw guarantee.

CPP




// CPP program to illustrate
// Implementation of pop() function
 
#include <iostream>
#include <stack>
using namespace std;
 
int main()
{
    stack<int> mystack;
    mystack.push(1);
    mystack.push(2);
    mystack.push(3);
    mystack.push(4);
    
   // Stack becomes 1, 2, 3, 4
 
    mystack.pop();
    mystack.pop();
  
   // Stack becomes 1, 2
 
    while (!mystack.empty()) {
        cout << ' ' << mystack.top();
        mystack.pop();
    }
}

Output

 2 1

Note: Here, output is printed on the basis of LIFO property.

Application: Given a number of integers, add them to the stack and find the size of the stack without using the size function. 

Input : 5, 13, 0, 9, 4
Output: 5

Approach: We can keep a counter variable that will keep track of the size of the stack. Whenever we push (add) elements into the stack then increment the counter that indicates the size of the stack has increased now and whenever we pop (remove) elements from the stack then decrement the counter that indicates decrement in size of the stack.

Algorithm: 

  • Push the given elements to the stack container one by one. 
  • Keep popping the elements of the stack until it becomes empty, and increment the counter variable. 
  • Print the counter variable.

CPP




// CPP program to illustrate
// Application of push()
// and pop() function
 
#include <iostream>
#include <stack>
using namespace std;
 
int main()
{
    int c = 0;
 
    // Empty stack
    stack<int> mystack;
    mystack.push(5);
    mystack.push(13);
    mystack.push(0);
    mystack.push(9);
    mystack.push(4);
    // stack becomes 5, 13, 0, 9, 4
 
    // Counting number of elements in queue
    while (!mystack.empty()) {
        mystack.pop();
        c++;
    }
    cout << c;
}

Output

5

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!