Priority Queue in C++ Programming
What is a Priority Queue in C++ ?
A Priority Queue in C++ programming is a advanced data structure, which is one step ahead of a normal queue. In priority queue we work with the data depending upon the priority of the data. Like, if we want to delete an element from the queue, the data with the highest priority will be deleted first, in the similar way if we are inserting some data in the queue, it will be arranged in the queue depending upon its priority. Let’s see how a priority queue in C++ works.
More About Priority Queue in C++ Programming
- As we’ve discussed above, Priority Queue is a special type of Queue, in which the data is arranges on the basis of its priority.
- There are two types of Priority Queue
- Minimum Priority Queue.
- Maximum Priority Queue
- Operations of enqueue(insertion) and dequeue(deletion) are performed on the basis of the priority of the data.
- A Priority Queue can be implemented using various different data structures like
- Using Arrays
- Using Linked List
- Using Heap, etc
- Enqueue(): This is the operation of inserting an element in a queue
- Dequeue(): This is the operation of deleting an element in a queue, based on the priority of the element.
- Print(): Print operation will print the all elements of the Queue.
How is a Priority Queue implemented in C++ ?
There are three different data structures that we can use to implement Priority Queue in C++:
- Linked List
Operations on Priority Queue
Operation can be perform on the Priority Queue:
- When we have to insert an element in Queue we use Enqueue() operation.
- Enqueue() operation is similar to that of a insertion operation in a data structure
- The difference between a normal insertion and enqueue operation in a Priority Queue is that in Priority Queue, data is inserted depending upon the priority of the data.
- When we have to remove an element from the Queue we use Dequeue() operation.
- Dequeue() operation similar to that of a deletion operation in a data structure.
- The difference between a normal deletion and dequeue operation in a Priority Queue is that in Priority Queue, data is deleted depending upon the priority of the data.
- Print() Is use to print or show the data and its priority of a Queue.
- isEmpty() function is use to check,Queue is empty or not.
Applications of Priority Queue
There is following applications of Priority Queue:
- Dijktra’s shortest path algorithm.
- Prim’s Algorithm
- Data Compression->Huffman Code
- Heap Sort
- Operating System: Local Balancing, Interrupt Handling.