C++ Program to Implement Priority Queue

«
»
This C++ Program demonstrates the implementation of Priority Queue.

Here is source code of the C++ Program to demonstrate the implementation of Priority Queue. 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
  3.  */
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10. /*
  11.  * Node Declaration
  12.  */
  13. struct node
  14. {
  15. 	int priority;
  16. 	int info;
  17. 	struct node *link;
  18. };
  19. /*
  20.  * Class Priority Queue
  21.  */
  22. class Priority_Queue
  23. {
  24.     private:
  25.         node *front;
  26.     public:
  27.         Priority_Queue()
  28.         {
  29.             front = NULL;
  30.         }
  31.         /*
  32.          * Insert into Priority Queue
  33.          */
  34.         void insert(int item, int priority)
  35.         {
  36.             node *tmp, *q;
  37.             tmp = new node;
  38.             tmp->info = item;
  39.             tmp->priority = priority;
  40.             if (front == NULL || priority < front->priority)
  41.             {
  42.                 tmp->link = front;
  43.                 front = tmp;
  44.             }
  45.             else
  46.             {
  47.                 q = front;
  48.                 while (q->link != NULL && q->link->priority <= priority)
  49.                     q=q->link;
  50.                 tmp->link = q->link;
  51.                 q->link = tmp;
  52.             }
  53.         }
  54.         /*
  55.          * Delete from Priority Queue
  56.          */
  57.         void del()
  58.         {
  59.             node *tmp;
  60.             if(front == NULL)
  61.                 cout<<"Queue Underflow\n";
  62.             else
  63.             {
  64.                 tmp = front;
  65.                 cout<<"Deleted item is: "<<tmp->info<<endl;
  66.                 front = front->link;
  67.                 free(tmp);
  68.             }
  69.         }
  70.         /*
  71.          * Print Priority Queue
  72.          */
  73.         void display()
  74.         {
  75.             node *ptr;
  76.             ptr = front;
  77.             if (front == NULL)
  78.                 cout<<"Queue is empty\n";
  79.             else
  80.             {	cout<<"Queue is :\n";
  81.                 cout<<"Priority       Item\n";
  82.                 while(ptr != NULL)
  83.                 {
  84.                     cout<<ptr->priority<<"                 "<<ptr->info<<endl;
  85.                     ptr = ptr->link;
  86.                 }
  87.             }
  88.         }
  89. };
  90. /*
  91.  * Main
  92.  */
  93. int main()
  94. {
  95.     int choice, item, priority;
  96.     Priority_Queue pq; 
  97.     do
  98.     {
  99.         cout<<"1.Insert\n";
  100.         cout<<"2.Delete\n";
  101.         cout<<"3.Display\n";
  102.         cout<<"4.Quit\n";
  103.         cout<<"Enter your choice : "; 
  104.         cin>>choice;
  105.         switch(choice)
  106.         {
  107.         case 1:
  108.             cout<<"Input the item value to be added in the queue : ";
  109.             cin>>item;
  110.             cout<<"Enter its priority : ";
  111.             cin>>priority;
  112.             pq.insert(item, priority);
  113.             break;
  114.         case 2:
  115.             pq.del();
  116.             break;
  117.         case 3:
  118.             pq.display();
  119.             break;
  120.         case 4:
  121.             break;
  122.         default :
  123.             cout<<"Wrong choice\n";
  124.         }
  125.     }
  126.     while(choice != 4);
  127.     return 0;
  128. }

$ g++ priority_queue.cpp
$ a.out
 
4.Quit
Enter your choice : 1
Input the item value to be added in the queue : 4
Enter its priority : 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the item value to be added in the queue : 3
Enter its priority : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the item value to be added in the queue : 2
Enter its priority : 4
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the item value to be added in the queue : 1
Enter its priority : 5
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue is :
Priority       Item
1                 5
2                 4
3                 3
4                 2
5                 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 2
Deleted item is: 5
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue is :
Priority       Item
2                 4
3                 3
4                 2
5                 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 4
 
------------------
(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.

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 & technical discussions at Telegram SanfoundryClasses.