C++ Program to Implement Stack in STL

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.

  1. /*
  2.  * C++ Program to Implement Stack in Stl
  3.  */
  4. #include <iostream>
  5. #include <stack>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     stack<int> st;
  12.     int choice, item;
  13.     while (1)
  14.     {
  15.         cout<<"\n---------------------"<<endl;
  16.         cout<<"Stack Implementation in Stl"<<endl;
  17.         cout<<"\n---------------------"<<endl;
  18.         cout<<"1.Insert Element into the Stack"<<endl;
  19.         cout<<"2.Delete Element from the Stack"<<endl;
  20. 	cout<<"3.Size of the Stack"<<endl;
  21.         cout<<"4.Top Element of the Stack"<<endl;
  22.         cout<<"5.Exit"<<endl;
  23.         cout<<"Enter your Choice: ";
  24.         cin>>choice;
  25.         switch(choice)
  26.         {
  27.         case 1:
  28.             cout<<"Enter value to be inserted: ";
  29.             cin>>item;
  30.             st.push(item);
  31.             break;
  32.         case 2:
  33.             item = st.top();
  34.             st.pop();
  35. 	    cout<<"Element "<<item<<" Deleted"<<endl;
  36.             break;
  37.         case 3:
  38. 	    cout<<"Size of the Queue: ";
  39. 	    cout<<st.size()<<endl;
  40.             break;
  41.         case 4:
  42. 	    cout<<"Top Element of the Stack: ";
  43. 	    cout<<st.top()<<endl;
  44.             break;
  45.         case 5:
  46.             exit(1);
  47. 	    break;
  48.         default:
  49.             cout<<"Wrong Choice"<<endl;
  50.         }
  51.     }
  52.     return 0;
  53. }

$ 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.

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.