Different ways to delete elements from Stack container in C++ STL
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
A Stack is a container in C++ STL that is based on the concept of Last In First Out (LIFO) context,i.e.the element which has been inserted recently will be on the top of stack and will be the first to get out.
Stack container in C++ STL uses Deque container internally. So, the pop() operation in stack actually calls pop_back() function in the internal deque container. We will explore ways to delete elements from a stack.
Now, here we are going to discuss methods to remove elements from stack:
1. stack::pop()
stack::pop() is a public member function that is used to remove the top-most element of the stack.
Since,there is only one end for insertion and deletion in stack,therefore the element added recently would be on the top of satck,and would be the first to get out of the stack.
Everytime,the size is decreased by one on performing pop operation on stack,until the stack is completely empty.
Few important points:
- pop() function does not require any parameter.
- On calling pop() function,it does not return any value.
- Time Complexity of performing pop is Constant time
Do you know that behind the stack,in the background another container is used that is hidden from outside world.This is deque container which supports stack.
The operations which happen behind the scene are:
- stack::push() --> deque::push_back()
- stack::pop() --> deque::pop_back()
- stack::top() --> deque::back()
Both implementation are bgiven below.First one is the simple implementation of stack::pop(),but in second one it has been done with deque::pop_back()
Implementation 1
In this implementation, we have created a stack with some initial elements (10, 20, 30, 40) and printed the top element by deleting the top element each time.
This demonstrates the use of pop() function in stack container in C++ STL.
// Part of OpenGenus
//Implementation of stack::pop()
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> st;
//Pushing element
st.push(10);
st.push(20);
st.push(30);
st.push(40);
cout<<"The front element is:"<<st.top()<<"\n";
//Displaying stack
cout<<"Input Stack is:\n";
while(!st.empty())
{
cout<<st.top()<<"\n";
st.pop(); //Removing top element every time
}
return 0;
}
Output 1
The front element is:40
Input Stack is:
40
30
20
10
Implementation 2
Following code example demonstrates deque which can behave as a stack where pop_back() functions acts same as pop() function in Stack container.
Internally, deque is used within a stack.
//Implementing stack with deque
#include<iostream>
#include<deque>
using namespace std;
int main()
{
deque <int> st;
//Pushing into stack
st.push_back(10);
st.push_back(20);
st.push_back(30);
st.push_back(40);
cout<<"The front element is:"<<st.back()<<"\n";
//pop from stack
cout<<"Input stack is:\n";
while(!st.empty())
{
cout<<st.back()<<"\n";
st.pop_back();
}
return 0;
}
Output 2
The front element is:40
Input stack is:
40
30
20
10
See,in both cases the output are same.
Note that we cannot use pop_back() function directly on a Stack container. It will raise a compilation error. Following will be the error:
opengenus.cpp: In function ‘int main()’:
opengenus.cpp:25:12: error: ‘class std::stack<int>’ has no member named ‘pop_back’
st.pop_back(); //Removing top element every time
^
To check the above error, consider the following code example:
// Part of OpenGenus
// Demonstrates we cannot use pop_back with stack
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> st;
//Pushing element
st.push(10);
st.push(20);
st.push(30);
st.push(40);
cout<<"The front element is:"<<st.top()<<"\n";
//Displaying stack
cout<<"Input Stack is:\n";
while(!st.empty())
{
cout<<st.top()<<"\n";
st.pop_back(); //Removing top element every time
}
return 0;
}
Hence, pop() is the only approach to delete an element from a stack container in C++ STL.
With this article at OpenGenus, you must have the complete idea of removing elements from stack in C++ STL. Enjoy.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.