C++ Program to Implement Queue in STL

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

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

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

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

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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.