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

advertisement
$ 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
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn