Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Easy
  • Last Updated : 22 Nov, 2021

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 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()

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 size function. 

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

Algorithm: 

  • Push the given elements to the stack container one by one. 
  • Keep popping the elements of 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 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!