C++ Program to Implement 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.

/*

* C++ Program to Implement Queue in Stl

*/

#include <iostream>

#include <queue>

#include <string>

#include <cstdlib>

usingnamespace std;

int main()

{

queue<int> q;

int choice, item;

while(1)

{

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

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

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

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

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

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

cout<<"4.Front Element of the Queue"<<endl;

cout<<"5.Last Element of the Queue"<<endl;

cout<<"6.Exit"<<endl;

cout<<"Enter your Choice: ";

cin>>choice;

switch(choice)

{

case1:

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

cin>>item;

q.push(item);

break;

case2:

item = q.front();

q.pop();

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

break;

case3:

cout<<"Size of the Queue: ";

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

break;

case4:

cout<<"Front Element of the Queue: ";

cout<<q.front()<<endl;

break;

case5:

cout<<"Back Element of the Queue: ";

cout<<q.back()<<endl;

break;

case6:

exit(1);

break;

default:

cout<<"Wrong Choice"<<endl;

}

}

return0;

}

advertisements

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

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