
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Priority Queue
The queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first.
Queue operations
- EnQueue: Insertion at rear end.
- DeQueue(): Deletion from front end.
But a priority queue doesn't follow First-In-First-Out, but rather than each element has a priority based on the level of importance.
- Items with the same priority are processed on First-In-First-Out service basis.
- An item with higher priority is processed before other items with lower priority.
Priority Queue Implementation Steps
The following are the steps to implement a priority queue in C++:
- First of all, you need to create a class named PriorityQueue.
- Then, initialize an empty list to store items and their priorities.
- Define a function insert(data, priority) to add an item. If the queue is empty, insert the item at the beginning. If the queue is not empty i.e., has items, find the correct position. Insert the new item after items with the same priority. Insert it before items with lower priority.
- Define a function del() to remove an item. If the queue is empty, print an error message "Underflow". If queue is not empty, remove the item from the front of the queue.
- You can also add a function to print the current state of the queue (optional, you can use it if you want to print the current state).
Class Description
Begin class Priority_Queue has following functions: function insert() to insert items at priority queue with their priorities: 1) If queue is empty insert data from the left end of the queue. 2) If queue is having some nodes then insert the new node at the end of those nodes having priority same with the new node and also before all the nodes having priority lesser than the current priority of the new node. function del() to delete items from queue. If queue is completely empty, print underflow otherwise delete the front element and update front. End
C++ Program to Implement Priority Queue
Following is an example of priority queue that shows how priority queue works over insertion, deletion, display, and exit:
#include<iostream> using namespace std; struct n { int p; int info; struct n *l; }; class Priority_Queue { private: // front pointer n *f; public: // constructor Priority_Queue() { f = NULL; } void insert(int i, int p) { n *t, *q; t = new n; t->info = i; t->p = p; t->l = NULL; if (f == NULL || p < f->p) { t->l = f; f = t; } else { q = f; while (q->l != NULL && q->l->p <= p) { q = q->l; } t->l = q->l; q->l = t; } } void del() { if (f == NULL) { cout << "Queue Underflow\n"; } else { n *t = f; cout << "Deleted item is: " << t->info << endl; f = f->l; // use delete instead of free in C++ delete t; } } void show() { if (f == NULL) { cout << "Queue is empty\n"; } else { n *ptr = f; cout << "Queue is:\n"; cout << "Priority Item\n"; while (ptr != NULL) { cout << ptr->p << " " << ptr->info << endl; ptr = ptr->l; } } } }; int main() { int c, i, p; Priority_Queue pq; do{ cout << "1.Insert\n"; cout << "2.Delete\n"; cout << "3.Display\n"; cout << "4.Exit\n"; cout << "Enter your choice: "; cin >> c; switch (c){ case 1: cout << "Input the item value to be added in the queue: "; cin >> i; cout << "Enter its priority: "; cin >> p; pq.insert(i, p); break; case 2: pq.del(); break; case 3: pq.show(); break; case 4: cout << "Exiting...\n"; break; default: cout << "Wrong choice\n"; } }while (c != 4); return 0; }
The above program produces the following result:
1.Insert 2.Delete 3.Display 4.Exit Enter your choice : 1 Input the item value to be added in the queue : 7 Enter its priority : 2 1.Insert 2.Delete 3.Display 4.Exit Enter your choice : 1 Input the item value to be added in the queue : 6 Enter its priority : 1 1.Insert 2.Delete 3.Display 4.Exit 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.Exit Enter your choice : 1 Input the item value to be added in the queue : 4 Enter its priority : 3 1.Insert 2.Delete 3.Display 4.Exit Enter your choice : 3 Queue is : Priority Item 1 6 2 7 3 3 3 4 1.Insert 2.Delete 3.Display 4.Exit Enter your choice : 4
Advertisements