A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/dsa/priority-queue-set-1-introduction/ below:

What is Priority Queue | Introduction to Priority Queue

What is Priority Queue | Introduction to Priority Queue

Last Updated : 23 Jul, 2025

Try it on GfG Practice

A priority queue is a type of queue that arranges elements based on their priority values.

For example, 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. 

Types of Priority Queue Types of Priority Queue

The following is an example of Descending order Priority Queue.

How Priority is Determined 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 on a Priority Queue

A typical priority queue supports the following operations:

1) Insertion : If the newly inserted item is of the highest priority, then it is inserted at the top. Otherwise, it is inserted in such a way that it is accessible after all higher priority items are accessed.

2) Deletion : We typically remove the highest priority item which is typically available at the top. Once we remove this item, we need not move next priority item at the top.

3) Peek : This operation only returns the highest priority item (which is typically available at the top) and does not make any change to the priority queue.

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.

Library Implementation of Priority Queue
  1. Priority Queue in C++
  2. Priority Queue in Java
  3. Priority Queue in Python
  4. Priority Queue in JavaScript
  5. Priority Queue in C#
Implementation of Priority Queue

Priority queue can be implemented using the following data structures: 

1) Implement Priority Queue Using Array: 

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

struct item {
        int item;
        int priority;
}

Arrays

enqueue()

dequeue()

peek()

Time Complexity

O(1)

O(n)

O(n)

Please Refer to Priority Queue using Array for more details.

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:

Linked List

push()

pop()

peek()

Time Complexity

 O(n)   

 O(1)  

O(1)

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. 

Please Refer to Priority Queue using Linked List for more details.

3) Implement Priority Queue Using Heap: 

A Binary Heap is ideal for priority queue implementation as it offers better performance than arrays or linked lists. The largest key is at the top and can be removed in O(log n) time, with the heap property restored efficiently. If a new entry is inserted immediately, its O(log n) insertion time may overlap with restoration. Since heaps use contiguous storage, they efficiently handle large datasets while ensuring logarithmic time for insertions and deletions. Operations on Binary Heap are as follows: 

Binary Heap

insert()

remove()

peek()

Time Complexity

O(log n)

O(log n)

O(1)

Please Refer Priority Queue using Heap EImplementation for more details

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.

Binary Search Tree peek() insert() delete() Time Complexity O(1) O(log n) O(log n)

Please refer Why is Binary Heap Preferred over BST for Priority Queue? for details.

Problems Based on Priority Queue or Heap

The following list of top coding problems on Heap covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.

Top 50 Problems on Heap Data Structure asked in SDE Interviews

Applications of Priority Queue 

Please refer Applications of Priority Queue for more details.

Advantages of Priority Queue Disadvantages of Priority Queue

See also: 

  1. Recent articles on Priority Queue!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4