C++ Program to Implement Priority_queue in STL

«
»
This C++ Program demonstrates implementation of Priority_queue in STL.

Here is source code of the C++ Program to demonstrate Priority_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 Priority_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.     priority_queue<int> pq;
  12.     int choice, item;
  13.     while (1)
  14.     {
  15.         cout<<"\n---------------------"<<endl;
  16.         cout<<"Priority Queue Implementation in Stl"<<endl;
  17.         cout<<"\n---------------------"<<endl;
  18.         cout<<"1.Insert Element into the Priority Queue"<<endl;
  19.         cout<<"2.Delete Element from the Priority Queue"<<endl;
  20. 	cout<<"3.Size of the Priority Queue"<<endl;
  21.         cout<<"4.Top Element of the Priority Queue"<<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.             pq.push(item);
  31.             break;
  32.         case 2:
  33.             item = pq.top();
  34.             if (!pq.empty())
  35.             {
  36.                 pq.pop();
  37.                 cout<<"Element "<<item<<" Deleted"<<endl;
  38.             }
  39.             else
  40.             {
  41.                 cout<<"Priority Queue is Empty"<<endl;
  42.             }
  43.             break;
  44.         case 3:
  45.             cout<<"Size of the Queue: ";
  46. 	    cout<<pq.size()<<endl;
  47.             break;
  48.         case 4:
  49.             cout<<"Top Element of the Queue: ";
  50.             cout<<pq.top()<<endl;
  51.             break;
  52.         case 5:
  53.             exit(1);
  54. 	    break;
  55.         default:
  56.             cout<<"Wrong Choice"<<endl;
  57.         }
  58.     }
  59.     return 0;
  60. }

advertisement
$ g++ priority_queue.cpp
$ a.out
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Enter value to be inserted: 5
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Enter value to be inserted: 3
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Enter value to be inserted: 7
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Enter value to be inserted: 4
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Enter value to be inserted: 2
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 1
Enter value to be inserted: 8
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 3
Size of the Queue: 6
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 4
Top Element of the Queue: 8
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 2
Element 8 Deleted
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 3
Size of the Queue: 5
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 4
Top Element of the Queue: 7
 
---------------------
Priority Queue Implementation in Stl
 
---------------------
1.Insert Element into the Priority Queue
2.Delete Element from the Priority Queue
3.Size of the Priority Queue
4.Top Element of the Priority Queue
5.Exit
Enter your Choice: 5
 
 
------------------
(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