Updated June 28, 2023
Introduction to Stack in C ++
Stacks in C ++ programming language play an essential role in LIFO (Last in first out ) context, meaning elements are inserted and extracted only from one end. SStacks are a type of container adaptor in which a new element is added at one end (top), and an element removed from that same end-only is called a stack. It’s an abstract data structure used to collect elements in the stack by following the LIFO mechanism. The element entered last in the stack will be deleted first because there is only one end for it.
Syntax:
template <
class C,
class Container = std::deque<C>
> class stack;
The std::stack class is a container adapter, a LIFO (last-in, first-out) data structure.
This class template will be a wrapper to the container with a specified set of functions. The stack pushes and pops the element from the back of the defined container, known as the top of the stack.
How does stack work in C++?
To see the working of the stack in a programming language, let’s see the list of functions associated with the stack. Given below are the functions:
- push(g): This function will add/insert the element or data value ‘g’ at the top of the stack. The Time Complexity of this function is O(1).
- pop(): This function will remove/delete the element or data value at the top of the stack. The Time Complexity of this function is O(1). It will delete the topmost element of the stack.
- top(): This function will return a reference to the topmost element of the stack. The time complexity of this function is O(1).
- size(): This function will return the size of the stack container, that is, the total number of elements stored in the stack. The time complexity of this function is O(1).
- empty(): This function will check whether the stack container is empty or not. Similarly, the time complexity for this function is O(1).
Examples of Stack in C++
Here we will see how actually a stack work in C++ programming language through C++ codes. Therefore, Let’s look at some programming examples in C++ language to explain the working of the stack.
Example #1
C ++ code to demonstrate the working of the stack in C ++ programming language:
Code:
#include <iostream>
#include <stack>
using namespace std;
void stackone ( stack < int > so)
{
stack < int > sg = so;
while ( !sg.empty() )
{
cout << '\t' << sg.top();
sg.pop();
}
}
int main ()
{
stack < int > nest;
nest.push ( 505 );
nest.push ( 404 );
nest.push ( 303 );
nest.push ( 202 );
nest.push ( 101 );
cout << "The stack nest is consists of these numbers: ";
stackone ( nest );
cout << "\n nest.size() : " << nest.size();
cout << "\n nest.top() : " << nest.top();
cout << "\n nest.pop() : ";
nest.pop();
stackone ( nest );
return 0;
}
Output:
In the above C++ code, you can see how stack functions like push and pop are used to push and pop values in and out of the container, which follows LIFO (Last in First out ) mechanism, as you can see the values in the output after pushing and popping the value. This code also shows the size of the stack container after performing all the operations. It shows the topmost elements and popped-out elements.
Example #2
C ++ code to demonstrate the working of the stack in C ++ programming language:
Code:
#include <iostream>
using namespace std;
int stack[100], x =100, top =- 1;
void push ( int value ) {
if ( top >= x-1)
cout << " This is Stack Overflow " << endl;
else {
top++;
stack [top] = value;
}
}
void pop() {
if ( top <= -1)
cout << " This is Stack Underflow " << endl;
else {
cout << " The popped element from the stack is "<< stack[top] << endl;
top--;
}
}
void show() {
if ( top >= 0) {
cout << " Stack elements are: " ;
for ( int i =top; i >= 0; i--)
cout << stack[i] << " ";
cout << endl;
} else
cout << " Stack is empty ";
}
int main() {
int ch, value;
cout << " 1) Do you want to Push in stack " << endl;
cout << " 2) Do you want to Pop from stack " << endl;
cout << " 3) Do you want to show stack " << endl;
cout << " 4) Do you want to Exit " << endl;
do {
cout << " Please enter the choice: " << endl;
cin >> ch;
switch ( ch ) {
case 1: {
cout << " Please enter the value to be pushed: " << endl;
cin >> value;
push ( value );
break;
}
case 2: {
pop();
break;
}
case 3: {
show();
break;
}
case 4: {
cout << " Exit " << endl;
break;
}
default: {
cout << " Invalid Choice " << endl;
}
}
}while ( ch != 4);
return 0;
}
Output:
In the above C++ code, you can see how stack functions like push and pop are used to push and pop values in and out of the container by adding underlying conditions following the LIFO ( Last in First out ) mechanism as you can perform all the 4 operations mentioned in the code. This code allows users to insert, delete, show, and exit elements from the stack container in an easy process flow.
Conclusion
Stack is handy when working on large programs because when you have a large set of data in the program, sometimes it gets difficult to find and erase data available in the program. Therefore, programmers use the stack to handle data in programming with ease.
Recommended Articles
This is a guide to Stack in C++. Here we discuss the introduction, how stack works in C++ with examples and code implementation. You may also have a look at the following articles to learn more –