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.

Priority Queue

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

Priority
Queue
Operation

  • 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++:

  • Array
  • Linked List
  • Heap

Operations on Priority Queue

Operation can be perform on the Priority Queue:

  • Enqueue()
  • Dequeue()
  • Print()
  • isEmpty()
Operation on priority queue in c++

1.Enqueue()

  • 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.
Insertion of an element in Priority Queue

2.Dequeue()

  • 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.
Deletion of a element in Priority Queue

3.Print()

  • Print() Is use to print or show the data and its priority of a Queue.
C++ Program for Printing a Queue

4.IsEmpty()

  • isEmpty() function is use to check,Queue is empty or not.
C++ Program for implementing isEmpty() in a queue

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.