LAST UPDATED: NOVEMBER 1, 2020
C++ STL Stack Program
Hello Everyone!
In this tutorial, we will learn about the working of a Stack and its implementation in the C++ programming language.
To understand the basic functionality of the Stack, we will recommend you to visit the Stack Data Structure, where we have explained this concept in detail from scratch.
For a better understanding of its implementation, refer to the well-commented C++ code given below.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//Function to print the elements of the stack
void show(stack<int> s)
{
while (!s.empty())
{
cout << " " << s.top(); //printing the topmost element
s.pop(); //removing the topmost element to bring next element at the top
}
cout << endl;
}
int main()
{
cout << "\n\nWelcome to Studytonight :-)\n\n\n";
cout << " ===== Program to demonstrate the working of Stacks, in CPP ===== \n\n";
int i;
//Stack declaration (stack of integers)
stack<int> s;
//Filling the elements by using the push() method.
cout << "Filling the Stack in LIFO order:"; //LIFO= Last In First Out
for (i = 1; i <= 5; i++)
{
s.push(i * 10); //inserting elements to the top
}
cout << "\n\nThe top-most element of the Stack is : " << s.top();
cout << "\n\nThe size of the Stack is : " << s.size();
cout << "\n\nThe elements of the Stack in LIFO order are: ";
show(s);
cout << "\n\nAfter deleting the top-most element from the stack, it becomes: ";
s.pop();
show(s);
cout << "\n\n\n";
return 0;
}
Output:
We hope that this post helped you develop a better understanding of the concept of Stack and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.
Keep Learning : )