# What is Priority Queue | Introduction to Priority Queue

• Difficulty Level : Easy
• Last Updated : 29 Sep, 2022

Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed). If in any case the elements have same priority, they are served as per their ordering in the queue.

Therefore all the elements are either arranged in an ascending or descending order.

## Properties of Priority Queue

So, a priority Queue is an extension of the queue with the following properties.

• Every item has a priority associated with it.
• An element with high priority is dequeued before an element with low priority.
• If two elements have the same priority, they are served according to their order in the queue.

In the below priority queue, an element with a maximum ASCII value will have the highest priority. The elements with higher priority are served first. ## How is Priority assigned to the elements in a Priority Queue?

In a priority queue, generally, the value of an element is considered for assigning the priority.

For example, the element with the highest value is assigned the highest priority and the element with the lowest value is assigned the lowest priority. The reverse case can also be used i.e., the element with the lowest value can be assigned the highest priority. Also, the priority can be assigned according to our needs.

## Operations of a Priority Queue:

A typical priority queue supports the following operations:

### 1) Insertion in a Priority Queue

When a new element is inserted in a priority queue, it moves to the empty slot from top to bottom and left to right. However, if the element is not in the correct place then it will be compared with the parent node. If the element is not in the correct order, the elements are swapped. The swapping process continues until all the elements are placed in the correct position.

### 2) Deletion in a Priority Queue

As you know that in a max heap, the maximum element is the root node. And it will remove the element which has maximum priority first. Thus, you remove the root node from the queue. This removal creates an empty slot, which will be further filled with new insertion. Then, it compares the newly inserted element with all the elements inside the queue to maintain the heap invariant.

### 3) Peek in a Priority Queue

This operation helps to return the maximum element from Max Heap or the minimum element from Min Heap without deleting the node from the priority queue.

## Types of Priority Queue:

### 1) Ascending Order Priority Queue

As the name suggests, in ascending order priority queue, the element with a lower priority value is given a higher priority in the priority list. For example, if we have the following elements in a priority queue arranged in ascending order like 4,6,8,9,10. Here, 4 is the smallest number, therefore, it will get the highest priority in a priority queue.

### 2) Descending order Priority Queue

The root node is the maximum element in a max heap, as you may know. It will also remove the element with the highest priority first. As a result, the root node is removed from the queue. This deletion leaves an empty space, which will be filled with fresh insertions in the future. The heap invariant is then maintained by comparing the newly inserted element to all other entries in the queue. Types of Priority Queues

## Difference between Priority Queue and Normal Queue?

There is no priority attached to elements in a queue, the rule of first-in-first-out(FIFO) is implemented whereas, in a priority queue, the elements have a priority. The elements with higher priority are served first.

## How to Implement Priority Queue?

Priority queue can be implemented using the following data structures:

• Arrays
• Heap data structure
• Binary search tree

Let’s discuss all these in detail.

### 1) Implement Priority Queue Using Array:

A simple implementation is to use an array of the following structure.

struct item {
int item;
int priority;
}

• enqueue(): This function is used to insert new data into the queue.
• dequeue(): This function removes the element with the highest priority from the queue.
• peek()/top(): This function is used to get the highest priority element in the queue without removing it from the queue.

## C++

 `// C++ program to implement Priority Queue``// using Arrays``#include ``using` `namespace` `std;` `// Structure for the elements in the``// priority queue``struct` `item {``    ``int` `value;``    ``int` `priority;``};` `// Store the element of a priority queue``item pr;` `// Pointer to the last index``int` `size = -1;` `// Function to insert a new element``// into priority queue``void` `enqueue(``int` `value, ``int` `priority)``{``    ``// Increase the size``    ``size++;` `    ``// Insert the element``    ``pr[size].value = value;``    ``pr[size].priority = priority;``}` `// Function to check the top element``int` `peek()``{``    ``int` `highestPriority = INT_MIN;``    ``int` `ind = -1;` `    ``// Check for the element with``    ``// highest priority``    ``for` `(``int` `i = 0; i <= size; i++) {` `        ``// If priority is same choose``        ``// the element with the``        ``// highest value``        ``if` `(highestPriority == pr[i].priority && ind > -1``            ``&& pr[ind].value < pr[i].value) {``            ``highestPriority = pr[i].priority;``            ``ind = i;``        ``}``        ``else` `if` `(highestPriority < pr[i].priority) {``            ``highestPriority = pr[i].priority;``            ``ind = i;``        ``}``    ``}` `    ``// Return position of the element``    ``return` `ind;``}` `// Function to remove the element with``// the highest priority``void` `dequeue()``{``    ``// Find the position of the element``    ``// with highest priority``    ``int` `ind = peek();` `    ``// Shift the element one index before``    ``// from the position of the element``    ``// with highest priority is found``    ``for` `(``int` `i = ind; i < size; i++) {``        ``pr[i] = pr[i + 1];``    ``}` `    ``// Decrease the size of the``    ``// priority queue by one``    ``size--;``}` `// Driver Code``int` `main()``{``    ``// Function Call to insert elements``    ``// as per the priority``    ``enqueue(10, 2);``    ``enqueue(14, 4);``    ``enqueue(16, 4);``    ``enqueue(12, 3);` `    ``// Stores the top element``    ``// at the moment``    ``int` `ind = peek();` `    ``cout << pr[ind].value << endl;` `    ``// Dequeue the top element``    ``dequeue();` `    ``// Check the top element``    ``ind = peek();``    ``cout << pr[ind].value << endl;` `    ``// Dequeue the top element``    ``dequeue();` `    ``// Check the top element``    ``ind = peek();``    ``cout << pr[ind].value << endl;` `    ``return` `0;``}`

## Java

 `// Java program to implement Priority Queue``// using Arrays``import` `java.util.*;` `// Structure for the elements in the``// priority queue``class` `item {``  ``public` `int` `value;``  ``public` `int` `priority;``};` `class` `GFG {` `  ``// Store the element of a priority queue``  ``static` `item[] pr = ``new` `item[``100000``];` `  ``// Pointer to the last index``  ``static` `int` `size = -``1``;``  ``// Function to insert a new element``  ``// into priority queue``  ``static` `void` `enqueue(``int` `value, ``int` `priority)``  ``{``    ``// Increase the size``    ``size++;` `    ``// Insert the element``    ``pr[size] = ``new` `item();``    ``pr[size].value = value;``    ``pr[size].priority = priority;``  ``}` `  ``// Function to check the top element``  ``static` `int` `peek()``  ``{``    ``int` `highestPriority = Integer.MIN_VALUE;``    ``int` `ind = -``1``;` `    ``// Check for the element with``    ``// highest priority``    ``for` `(``int` `i = ``0``; i <= size; i++) {` `      ``// If priority is same choose``      ``// the element with the``      ``// highest value``      ``if` `(highestPriority == pr[i].priority``          ``&& ind > -``1``          ``&& pr[ind].value < pr[i].value) {``        ``highestPriority = pr[i].priority;``        ``ind = i;``      ``}``      ``else` `if` `(highestPriority < pr[i].priority) {``        ``highestPriority = pr[i].priority;``        ``ind = i;``      ``}``    ``}` `    ``// Return position of the element``    ``return` `ind;``  ``}` `  ``// Function to remove the element with``  ``// the highest priority``  ``static` `void` `dequeue()``  ``{``    ``// Find the position of the element``    ``// with highest priority``    ``int` `ind = peek();` `    ``// Shift the element one index before``    ``// from the position of the element``    ``// with highest priority is found``    ``for` `(``int` `i = ind; i < size; i++) {``      ``pr[i] = pr[i + ``1``];``    ``}` `    ``// Decrease the size of the``    ``// priority queue by one``    ``size--;``  ``}` `  ``public` `static` `void` `main(String[] args)``  ``{``    ``// Function Call to insert elements``    ``// as per the priority``    ``enqueue(``10``, ``2``);``    ``enqueue(``14``, ``4``);``    ``enqueue(``16``, ``4``);``    ``enqueue(``12``, ``3``);` `    ``// Stores the top element``    ``// at the moment``    ``int` `ind = peek();` `    ``System.out.println(pr[ind].value);` `    ``// Dequeue the top element``    ``dequeue();` `    ``// Check the top element``    ``ind = peek();``    ``System.out.println(pr[ind].value);` `    ``// Dequeue the top element``    ``dequeue();` `    ``// Check the top element``    ``ind = peek();``    ``System.out.println(pr[ind].value);``  ``}``}` `// this code is contributed by phasing17`

## Python3

 `import` `sys` `# Structure for the elements in the``# priority queue``class` `item :``    ``value ``=` `0``    ``priority ``=` `0``class` `GFG :``  ` `    ``# Store the element of a priority queue``    ``pr ``=` `[``None``] ``*` `(``100000``)``    ` `    ``# Pointer to the last index``    ``size ``=` `-``1``    ` `    ``# Function to insert a new element``    ``# into priority queue``    ``@staticmethod``    ``def` `enqueue( value,  priority) :``      ` `        ``# Increase the size``        ``GFG.size ``+``=` `1``        ` `        ``# Insert the element``        ``GFG.pr[GFG.size] ``=` `item()``        ``GFG.pr[GFG.size].value ``=` `value``        ``GFG.pr[GFG.size].priority ``=` `priority``        ` `    ``# Function to check the top element``    ``@staticmethod``    ``def`  `peek() :``        ``highestPriority ``=` `-``sys.maxsize``        ``ind ``=` `-``1``        ` `        ``# Check for the element with``        ``# highest priority``        ``i ``=` `0``        ``while` `(i <``=` `GFG.size) :``          ` `            ``# If priority is same choose``            ``# the element with the``            ``# highest value``            ``if` `(highestPriority ``=``=` `GFG.pr[i].priority ``and` `ind > ``-``1` `and` `GFG.pr[ind].value < GFG.pr[i].value) :``                ``highestPriority ``=` `GFG.pr[i].priority``                ``ind ``=` `i``            ``elif``(highestPriority < GFG.pr[i].priority) :``                ``highestPriority ``=` `GFG.pr[i].priority``                ``ind ``=` `i``            ``i ``+``=` `1``            ` `        ``# Return position of the element``        ``return` `ind``      ` `    ``# Function to remove the element with``    ``# the highest priority``    ``@staticmethod``    ``def` `dequeue() :``      ` `        ``# Find the position of the element``        ``# with highest priority``        ``ind ``=` `GFG.peek()``        ` `        ``# Shift the element one index before``        ``# from the position of the element``        ``# with highest priority is found``        ``i ``=` `ind``        ``while` `(i < GFG.size) :``            ``GFG.pr[i] ``=` `GFG.pr[i ``+` `1``]``            ``i ``+``=` `1``            ` `        ``# Decrease the size of the``        ``# priority queue by one``        ``GFG.size ``-``=` `1``    ``@staticmethod``    ``def` `main( args) :``      ` `        ``# Function Call to insert elements``        ``# as per the priority``        ``GFG.enqueue(``10``, ``2``)``        ``GFG.enqueue(``14``, ``4``)``        ``GFG.enqueue(``16``, ``4``)``        ``GFG.enqueue(``12``, ``3``)``        ` `        ``# Stores the top element``        ``# at the moment``        ``ind ``=` `GFG.peek()``        ``print``(GFG.pr[ind].value)``        ` `        ``# Dequeue the top element``        ``GFG.dequeue()``        ` `        ``# Check the top element``        ``ind ``=` `GFG.peek()``        ``print``(GFG.pr[ind].value)``        ` `        ``# Dequeue the top element``        ``GFG.dequeue()``        ` `        ``# Check the top element``        ``ind ``=` `GFG.peek()``        ``print``(GFG.pr[ind].value)``    ` `if` `__name__``=``=``"__main__"``:``    ``GFG.main([])``    ` `    ``# This code is contributed by aadityaburujwale.`

## C#

 `// C# program to implement Priority Queue``// using Arrays` `using` `System;` `// Structure for the elements in the``// priority queue``public` `class` `item {``    ``public` `int` `value;``    ``public` `int` `priority;``};`  `public` `class` `GFG``{``    ` `    ``// Store the element of a priority queue``    ``static` `item[] pr = ``new` `item;` `    ``// Pointer to the last index``    ``static` `int` `size = -1;``    ``// Function to insert a new element``    ``// into priority queue``    ``static` `void` `enqueue(``int` `value, ``int` `priority)``    ``{``        ``// Increase the size``        ``size++;``    ` `        ``// Insert the element``        ``pr[size] = ``new` `item();``        ``pr[size].value = value;``        ``pr[size].priority = priority;``    ``}``    ` `    ``// Function to check the top element``    ``static` `int` `peek()``    ``{``        ``int` `highestPriority =  ``int``.MinValue;``        ``int` `ind = -1;``    ` `        ``// Check for the element with``        ``// highest priority``        ``for` `(``int` `i = 0; i <= size; i++) {``    ` `            ``// If priority is same choose``            ``// the element with the``            ``// highest value``            ``if` `(highestPriority == pr[i].priority && ind > -1``                ``&& pr[ind].value < pr[i].value) {``                ``highestPriority = pr[i].priority;``                ``ind = i;``            ``}``            ``else` `if` `(highestPriority < pr[i].priority) {``                ``highestPriority = pr[i].priority;``                ``ind = i;``            ``}``        ``}``    ` `        ``// Return position of the element``        ``return` `ind;``    ``}``    ` `    ``// Function to remove the element with``    ``// the highest priority``    ``static` `void` `dequeue()``    ``{``        ``// Find the position of the element``        ``// with highest priority``        ``int` `ind = peek();``    ` `        ``// Shift the element one index before``        ``// from the position of the element``        ``// with highest priority is found``        ``for` `(``int` `i = ind; i < size; i++) {``            ``pr[i] = pr[i + 1];``        ``}``    ` `        ``// Decrease the size of the``        ``// priority queue by one``        ``size--;``    ``}` `    ``public` `static` `void` `Main(``string``[] args)``    ``{``         ``// Function Call to insert elements``        ``// as per the priority``        ``enqueue(10, 2);``        ``enqueue(14, 4);``        ``enqueue(16, 4);``        ``enqueue(12, 3);``    ` `        ``// Stores the top element``        ``// at the moment``        ``int` `ind = peek();``    ` `        ``Console.WriteLine(pr[ind].value);``    ` `        ``// Dequeue the top element``        ``dequeue();``    ` `        ``// Check the top element``        ``ind = peek();``        ``Console.WriteLine(pr[ind].value);``    ` `        ``// Dequeue the top element``        ``dequeue();``    ` `        ``// Check the top element``        ``ind = peek();``        ``Console.WriteLine(pr[ind].value);``    ``}``}` `//this code is contributed by phasing17`

## Javascript

 `// JavaScript program to implement Priority Queue``// using Arrays` `// Structure for the elements in the``// priority queue``class item {``    ``constructor()``    ``{``        ``this``.value;``        ``this``.priority;``    ``}``};` `// Store the element of a priority queue``let pr = [];``for` `(``var` `i = 0; i < 100000; i++)``    ``pr.push(``new` `item());` `// Pointer to the last index``let size = -1;` `// Function to insert a new element``// into priority queue``function` `enqueue(value, priority)``{``    ``// Increase the size``    ``size++;` `    ``// Insert the element``    ``pr[size] = ``new` `item();``    ``pr[size].value = value;``    ``pr[size].priority = priority;``}` `// Function to check the top element``function` `peek()``{``    ``let highestPriority = Number.MIN_SAFE_INTEGER;``    ``let ind = -1;` `    ``// Check for the element with``    ``// highest priority``    ``for` `(``var` `i = 0; i <= size; i++) {` `        ``// If priority is same choose``        ``// the element with the``        ``// highest value``        ``if` `(highestPriority == pr[i].priority && ind > -1``            ``&& pr[ind].value < pr[i].value) {``            ``highestPriority = pr[i].priority;``            ``ind = i;``        ``}``        ``else` `if` `(highestPriority < pr[i].priority) {``            ``highestPriority = pr[i].priority;``            ``ind = i;``        ``}``    ``}` `    ``// Return position of the element``    ``return` `ind;``}` `// Function to remove the element with``// the highest priority``function` `dequeue()``{``    ``// Find the position of the element``    ``// with highest priority``    ``let ind = peek();` `    ``// Shift the element one index before``    ``// from the position of the element``    ``// with highest priority is found``    ``for` `(``var` `i = ind; i < size; i++) {``        ``pr[i] = pr[i + 1];``    ``}` `    ``// Decrease the size of the``    ``// priority queue by one``    ``size--;``}` `// Function Call to insert elements``// as per the priority``enqueue(10, 2);``enqueue(14, 4);``enqueue(16, 4);``enqueue(12, 3);` `// Stores the top element``// at the moment``let ind = peek();` `console.log(pr[ind].value);` `// Dequeue the top element``dequeue();` `// Check the top element``ind = peek();``console.log(pr[ind].value);` `// Dequeue the top element``dequeue();` `// Check the top element``ind = peek();``console.log(pr[ind].value);` `// this code is contributed by phasing17`

Output

```16
14
12```

### 2) Implement Priority Queue Using Linked List:

In a LinkedList implementation, the entries are sorted in descending order based on their priority. The highest priority element is always added to the front of the priority queue, which is formed using linked lists. The functions like push(), pop(), and peek() are used to implement a priority queue using a linked list and are explained as follows:

• push(): This function is used to insert new data into the queue.
• pop(): This function removes the element with the highest priority from the queue.
• peek() / top(): This function is used to get the highest priority element in the queue without removing it from the queue.

## C++

 `// C++ code to implement Priority Queue``// using Linked List``#include ``using` `namespace` `std;` `// Node``typedef` `struct` `node {``    ``int` `data;` `    ``// Lower values indicate``    ``// higher priority``    ``int` `priority;` `    ``struct` `node* next;` `} Node;` `// Function to create a new node``Node* newNode(``int` `d, ``int` `p)``{``    ``Node* temp = (Node*)``malloc``(``sizeof``(Node));``    ``temp->data = d;``    ``temp->priority = p;``    ``temp->next = NULL;` `    ``return` `temp;``}` `// Return the value at head``int` `peek(Node** head) { ``return` `(*head)->data; }` `// Removes the element with the``// highest priority form the list``void` `pop(Node** head)``{``    ``Node* temp = *head;``    ``(*head) = (*head)->next;``    ``free``(temp);``}` `// Function to push according to priority``void` `push(Node** head, ``int` `d, ``int` `p)``{``    ``Node* start = (*head);` `    ``// Create new Node``    ``Node* temp = newNode(d, p);` `    ``// Special Case: The head of list has``    ``// lesser priority than new node``    ``if` `((*head)->priority < p) {` `        ``// Insert New Node before head``        ``temp->next = *head;``        ``(*head) = temp;``    ``}``    ``else` `{` `        ``// Traverse the list and find a``        ``// position to insert new node``        ``while` `(start->next != NULL``               ``&& start->next->priority > p) {``            ``start = start->next;``        ``}` `        ``// Either at the ends of the list``        ``// or at required position``        ``temp->next = start->next;``        ``start->next = temp;``    ``}``}` `// Function to check is list is empty``int` `isEmpty(Node** head) { ``return` `(*head) == NULL; }` `// Driver code``int` `main()``{` `    ``// Create a Priority Queue``    ``// 7->4->5->6``    ``Node* pq = newNode(4, 1);``    ``push(&pq, 5, 2);``    ``push(&pq, 6, 3);``    ``push(&pq, 7, 0);` `    ``while` `(!isEmpty(&pq)) {``        ``cout << ``" "` `<< peek(&pq);``        ``pop(&pq);``    ``}``    ``return` `0;``}`

Output

` 6 5 4 7`

Refer to this article for more details.

Note: We can also use Linked List, time complexity of all operations with linked list remains same as array. The advantage with linked list is deleteHighestPriority() can be more efficient as we don’t have to move items.

### 3) Implement Priority Queue Using Heaps:

Binary Heap is generally preferred for priority queue implementation because heaps provide better performance compared to arrays or LinkedList. Operations on Binary Heap are as follows:

• insert(p): Inserts a new element with priority p.
• extractMax(): Extracts an element with maximum priority.
• remove(i): Removes an element pointed by an iterator i.
• getMax(): Returns an element with maximum priority.
• changePriority(i, p): Changes the priority of an element pointed by i to p.

Refer to this article for code implementation.

### 4) Implement Priority Queue Using Binary Search Tree:

A Self-Balancing Binary Search Tree like AVL Tree, Red-Black Tree, etc. can also be used to implement a priority queue. Operations like peek(), insert() and delete() can be performed using BST.