This C++ Program demonstrates implementation of Stack in STL.

Here is source code of the C++ Program to demonstrate Stack in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C++ Program to Implement Stack in Stl`

`*/`

`#include <iostream>`

`#include <stack>`

`#include <string>`

`#include <cstdlib>`

using namespace std;

int main()

`{`

stack<int> st;

int choice, item;

while (1)

`{`

cout<<"\n---------------------"<<endl;

cout<<"Stack Implementation in Stl"<<endl;

cout<<"\n---------------------"<<endl;

cout<<"1.Insert Element into the Stack"<<endl;

cout<<"2.Delete Element from the Stack"<<endl;

cout<<"3.Size of the Stack"<<endl;

cout<<"4.Top Element of the Stack"<<endl;

cout<<"5.Exit"<<endl;

cout<<"Enter your Choice: ";

cin>>choice;

switch(choice)

`{`

case 1:

cout<<"Enter value to be inserted: ";

cin>>item;

st.push(item);

break;

case 2:

item = st.top();

st.pop();

cout<<"Element "<<item<<" Deleted"<<endl;

break;

case 3:

cout<<"Size of the Queue: ";

cout<<st.size()<<endl;

break;

case 4:

cout<<"Top Element of the Stack: ";

cout<<st.top()<<endl;

break;

case 5:

exit(1);

break;

default:

cout<<"Wrong Choice"<<endl;

`}`

`}`

return 0;

`}`

$ g++ stack.cpp $ a.out --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Enter value to be inserted: 2 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Enter value to be inserted: 3 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Enter value to be inserted: 4 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Enter value to be inserted: 5 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Enter value to be inserted: 6 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 4 Top Element of the Stack: 6 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 3 Size of the Queue: 5 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 2 Element 6 Deleted --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 4 Top Element of the Stack: 5 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 3 Size of the Queue: 4 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 1 Enter value to be inserted: 8 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 4 Top Element of the Stack: 8 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 3 Size of the Queue: 5 --------------------- Stack Implementation in Stl --------------------- 1.Insert Element into the Stack 2.Delete Element from the Stack 3.Size of the Stack 4.Top Element of the Stack 5.Exit Enter your Choice: 5 ------------------ (program exited with code: 1) Press return to continue

**Sanfoundry Global Education & Learning Series – 1000 C++ Programs.**

If you wish to look at all C++ Programming examples, go to C++ Programs.