Priority Queue using Linked List
Last Updated :
11 Feb, 2025
Implement Priority Queue using Linked Lists. The Linked List should be so created so that the highest priority ( priority is assigned from 0 to n-1 where n is the number of elements, where 0 means the highest priority and n-1 being the least ) element is always at the head of the list. The list is arranged in descending order of elements based on their priority. The Priority Queue should have the below functions
- push(): This function is used to insert a 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.
Examples:
Input: Values = 4 (priority = 1), 5 (priority = 2), 6 (priority = 3), 7 (priority = 0)
Output: 7 4 5 6
Explanation: Values get peeked and popped according to their priority in descending order.
This approach manages a priority queue using a linked list. The PUSH operation inserts nodes in order of priority, ensuring the highest priority node is always at the head. The POP operation removes the highest priority node, while PEEK returns the data of the highest priority node without removing it.
Follow these steps to solve the problem
PUSH(HEAD, DATA, PRIORITY):
- Step 1: Create new node with DATA and PRIORITY
- Step 2: Check if HEAD has lower priority. If true follow Steps 3-4 and end. Else goto Step 5.
- Step 3: NEW -> NEXT = HEAD
- Step 4: HEAD = NEW
- Step 5: Set TEMP to head of the list
- Step 6: While TEMP -> NEXT != NULL and TEMP -> NEXT -> PRIORITY > PRIORITY
- Step 7: TEMP = TEMP -> NEXT
[END OF LOOP] - Step 8: NEW -> NEXT = TEMP -> NEXT
- Step 9: TEMP -> NEXT = NEW
- Step 10: End
POP(HEAD):
- Step 1: Set the head of the list to the next node in the list. HEAD = HEAD -> NEXT.
- Step 2: Free the node at the head of the list
- Step 3: End
PEEK(HEAD):
- Step 1: Return HEAD -> DATA
- Step 2: End
C++
// C++ code to implement Priority Queue
// using Linked List
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
int priority;
Node* next;
Node(int x, int p) {
data = x;
priority = p;
next = NULL;
}
};
// Return the value at head
int peek(Node* head) {
// Return the data of the node at the head of the list
return head->data;
}
// Removes the element with the highest priority from the list
Node* pop(Node* head) {
// Store the current head node in a temporary variable
Node* temp = head;
// Move the head to the next node in the list
head = head->next;
// Free the memory of the removed head node
delete temp;
// Return the new head of the list
return head;
}
// Function to push according to priority
Node* push(Node* head, int d, int p) {
Node* start = head;
// Create new Node with the given data and priority
Node* temp = new Node(d, p);
// Special Case: Insert the new node before the head
// if the list is empty or the head has lower priority
if (head == NULL || head->priority > p) {
// Insert the new node before the head
temp->next = head;
head = temp;
}
else {
// Traverse the list to find the correct position
// to insert the new node based on priority
while (start->next != NULL &&
start->next->priority < p) {
start = start->next;
}
// Insert the new node at the found position
temp->next = start->next;
start->next = temp;
}
return head;
}
// Function to check if the list is empty
int isEmpty(Node* head) {
return (head == NULL);
}
// Driver code
int main() {
Node* pq = new Node(4, 1);
pq = push(pq, 5, 2);
pq = push(pq, 6, 3);
pq = push(pq, 7, 0);
while (!isEmpty(pq)) {
cout << " " << peek(pq);
pq = pop(pq);
}
return 0;
}
C
// C code to implement Priority Queue
// using Linked List
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
int priority;
struct node* next;
} 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 the data of the node at the head of the list
return head->data;
}
// Removes the element with the highest priority from the list
Node* pop(Node* head) {
// Store the current head node in a temporary variable
Node* temp = head;
// Move the head to the next node in the list
head = head->next;
// Free the memory of the removed head node
free(temp);
// Return the new head of the list
return head;
}
// Function to push according to priority
Node* push(Node* head, int d, int p) {
Node* start = head;
// Create new Node with the given data and priority
Node* temp = newNode(d, p);
// Special Case: Insert the new node before the head
// if the list is empty or the head has lower priority
if (head->priority > p) {
// Insert the new node before the head
temp->next = head;
head = temp;
}
else {
// Traverse the list to find the correct position
// to insert the new node based on priority
while (start->next != NULL &&
start->next->priority < p) {
start = start->next;
}
// Insert the new node at the found position
temp->next = start->next;
start->next = temp;
}
return head;
}
// Function to check if the list is empty
int isEmpty(Node* head) {
return (head == NULL);
}
// Driver code
int main() {
Node* pq = newNode(4, 1);
pq = push(pq, 5, 2);
pq = push(pq, 6, 3);
pq = push(pq, 7, 0);
while (!isEmpty(pq)) {
printf("%d ", peek(pq));
pq = pop(pq);
}
return 0;
}
Java
// Java code to implement Priority Queue
// using Linked List
import java.util.* ;
class GfG {
static class Node {
int data;
int priority;
Node next;
}
static Node node = new Node();
// Function to Create A New Node
static Node newNode(int d, int p) {
Node temp = new Node();
temp.data = d;
temp.priority = p;
temp.next = null;
return temp;
}
// Return the value at head
static int peek(Node head) {
return (head).data;
}
// Removes the element with the
// highest priority from the list
static Node pop(Node head) {
(head) = (head).next;
return head;
}
// Function to push according to priority
static Node 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. So insert new
// node before head node and change head 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;
}
return head;
}
// Function to check is list is empty
static int isEmpty(Node head) {
return ((head) == null)?1:0;
}
// Driver code
public static void main(String args[]) {
Node pq = newNode(4, 1);
pq =push(pq, 5, 2);
pq =push(pq, 6, 3);
pq =push(pq, 7, 0);
while (isEmpty(pq)==0) {
System.out.printf("%d ", peek(pq));
pq=pop(pq);
}
}
}
Python
# Python3 code to implement Priority Queue
# using Singly Linked List
class PriorityQueueNode:
def __init__(self, value, pr):
self.data = value
self.priority = pr
self.next = None
# Global variable for the front of the queue
front = None
# Method to check Priority Queue is Empty
def isEmpty():
return front is None
# Method to add items in Priority Queue
# According to their priority value
def push(value, priority):
global front
# Condition check for checking Priority
# Queue is empty or not
if isEmpty():
front = PriorityQueueNode(value, priority)
return 1
else:
# Special condition check to see that
# first node priority value
if front.priority > priority:
newNode = PriorityQueueNode(value, priority)
newNode.next = front
front = newNode
return 1
else:
temp = front
while temp.next:
if priority <= temp.next.priority:
break
temp = temp.next
newNode = PriorityQueueNode(value, priority)
newNode.next = temp.next
temp.next = newNode
return 1
# Method to remove high priority item
# from the Priority Queue
def pop():
global front
if isEmpty():
return
else:
front = front.next
return 1
# Method to return high priority node
# value without removing it
def peek():
if isEmpty():
return
else:
return front.data
# Method to traverse the Priority Queue
def traverse():
if isEmpty():
return "Queue is Empty!"
else:
temp = front
while temp:
print(temp.data, end=" ")
temp = temp.next
# Driver code
if __name__ == "__main__":
push(4, 1)
push(5, 2)
push(6, 3)
push(7, 0)
traverse()
pop()
C#
// C# code to implement Priority Queue
// using Linked List
using System;
public class GfG {
public class Node {
public int data;
public int priority;
public Node next;
}
public static Node node = new Node();
static Node newNode(int d, int p) {
Node temp = new Node();
temp.data = d;
temp.priority = p;
temp.next = null;
return temp;
}
// Return the value at head
static int peek(Node head) {
return (head).data;
}
// Removes the element with the
// highest priority from the list
static Node pop(Node head) {
(head) = (head).next;
return head;
}
// Function to push according to priority
static Node 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.
// So insert new node before head node
// and change head 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;
}
return head;
}
// Function to check is list is empty
static int isEmpty(Node head) {
return ((head) == null) ? 1 : 0;
}
// Driver code
public static void Main(string[] args) {
Node pq = newNode(4, 1);
pq = push(pq, 5, 2);
pq = push(pq, 6, 3);
pq = push(pq, 7, 0);
while (isEmpty(pq) == 0) {
Console.Write("{0:D} ", peek(pq));
pq = pop(pq);
}
}
}
JavaScript
// JavaScript code to implement Priority Queue
// using Linked List
class Node {
constructor() {
this.data = 0;
this.priority = 0;
this.next = null;
}
}
var node = new Node();
function newNode(d, p) {
var temp = new Node();
temp.data = d;
temp.priority = p;
temp.next = null;
return temp;
}
// Return the value at head
function peek(head) {
// Return the data of the node at the head of the list
return head.data;
}
// Removes the element with the highest priority from the list
function pop(head) {
// Store the current head node in a temporary variable
var temp = head;
// Move the head to the next node in the list
head = head.next;
// Return the new head of the list
return head;
}
// Function to push according to priority
function push(head, d, p) {
var start = head;
// Create new Node with the given data and priority
var temp = newNode(d, p);
// Special Case: Insert the new node before the head
// if the list is empty or the head has lower priority
if (head.priority > p) {
// Insert New Node before head
temp.next = head;
head = temp;
}
else {
// Traverse the list to find the correct position
// to insert the new node based on priority
while (start.next != null && start.next.priority <= p) {
start = start.next;
}
// Insert the new node at the found position
temp.next = start.next;
start.next = temp;
}
return head;
}
// Function to check if the list is empty
function isEmpty(head) {
return head == null ? 1 : 0;
}
// Driver code
var pq = newNode(4, 1);
pq = push(pq, 5, 2);
pq = push(pq, 6, 3);
pq = push(pq, 7, 0);
while (isEmpty(pq) == 0) {
console.log(peek(pq) + " ");
pq = pop(pq);
}
Time Complexity: push() -> O(n) , To insert an element we must traverse the list and find the proper position to insert the node . This makes the push() operation takes O(n) time.
pop -> O(1), as it is performed in constant time.
peek -> O(1), as it is performed in constant time.
Space Complexity: O(n), as we are making a List of size n
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem