Priority Queue using Doubly Linked List
Last Updated :
20 Mar, 2023
Given Nodes with their priority, implement a priority queue using doubly linked list.
Prerequisite : Priority Queue
- push(): This function is used to insert a new data into the queue.
- pop(): This function removes the element with the lowest priority value from the queue.
- peek() / top(): This function is used to get the lowest priority element in the queue without removing it from the queue.
Approach :
1. Create a doubly linked list having fields info(hold the information of the Node), priority(hold the priority of the Node), prev(point to previous Node), next(point to next Node).
2. Insert the element and priority in the Node.
3. Arrange the Nodes in the increasing order of priority.
Below is the implementation of above steps :
C++
// C++ code to implement priority
// queue using doubly linked list
#include <bits/stdc++.h>
using namespace std;
// Linked List Node
struct Node {
int info;
int priority;
struct Node *prev, *next;
};
// Function to insert a new Node
void push(Node** fr, Node** rr, int n, int p)
{
Node* news = (Node*)malloc(sizeof(Node));
news->info = n;
news->priority = p;
// If linked list is empty
if (*fr == NULL) {
*fr = news;
*rr = news;
news->next = NULL;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if (p <= (*fr)->priority) {
news->next = *fr;
(*fr)->prev = news->next;
*fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if (p > (*rr)->priority) {
news->next = NULL;
(*rr)->next = news;
news->prev = (*rr)->next;
*rr = news;
}
// Handle other cases
else {
// Find position where we need to
// insert.
Node* start = (*fr)->next;
while (start->priority > p)
start = start->next;
(start->prev)->next = news;
news->next = start->prev;
news->prev = (start->prev)->next;
start->prev = news->next;
}
}
}
// Return the value at rear
int peek(Node* fr) { return fr->info; }
bool isEmpty(Node* fr) { return (fr == NULL); }
// Removes the element with the
// least priority value from the list
int pop(Node** fr, Node** rr)
{
Node* temp = *fr;
int res = temp->info;
(*fr) = (*fr)->next;
free(temp);
if (*fr == NULL)
*rr = NULL;
return res;
}
// Driver code
int main()
{
Node *front = NULL, *rear = NULL;
push(&front, &rear, 2, 3);
push(&front, &rear, 3, 4);
push(&front, &rear, 4, 5);
push(&front, &rear, 5, 6);
push(&front, &rear, 6, 7);
push(&front, &rear, 1, 2);
cout << pop(&front, &rear) << endl;
cout << peek(front);
return 0;
}
C
// C code to implement priority
// queue using doubly linked list
#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct Node {
int info;
int priority;
struct Node *prev, *next;
};
// Function to insert a new Node
void push(struct Node** fr, struct Node** rr, int n, int p)
{
struct Node* news
= (struct Node*)malloc(sizeof(struct Node*));
news->info = n;
news->priority = p;
// If linked list is empty
if (*fr == NULL) {
*fr = news;
*rr = news;
news->next = NULL;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if (p <= (*fr)->priority) {
news->next = *fr;
(*fr)->prev = news->next;
*fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if (p > (*rr)->priority) {
news->next = NULL;
(*rr)->next = news;
news->prev = (*rr)->next;
*rr = news;
}
// Handle other cases
else {
// Find position where we need to
// insert.
struct Node* start = (*fr)->next;
while (start->priority > p)
start = start->next;
(start->prev)->next = news;
news->next = start->prev;
news->prev = (start->prev)->next;
start->prev = news->next;
}
}
}
// Return the value at rear
int peek(struct Node* fr) { return fr->info; }
int isEmpty(struct Node* fr) { return (fr == NULL); }
// Removes the element with the
// least priority value from the list
int pop(struct Node** fr, struct Node** rr)
{
struct Node* temp = *fr;
int res = temp->info;
(*fr) = (*fr)->next;
free(temp);
if (*fr == NULL)
*rr = NULL;
return res;
}
// Driver code
int main()
{
struct Node *front = NULL, *rear = NULL;
push(&front, &rear, 2, 3);
push(&front, &rear, 3, 4);
push(&front, &rear, 4, 5);
push(&front, &rear, 5, 6);
push(&front, &rear, 6, 7);
push(&front, &rear, 1, 2);
printf("%d\n", pop(&front, &rear));
printf("%d\n", peek(front));
return 0;
}
Java
// Java code to implement priority
// queue using doubly linked list
import java.util.*;
class Solution {
static Node front, rear;
// Linked List Node
static class Node {
int info;
int priority;
Node prev, next;
}
// Function to insert a new Node
static void push(Node fr, Node rr, int n, int p)
{
Node news = new Node();
news.info = n;
news.priority = p;
// If linked list is empty
if (fr == null) {
fr = news;
rr = news;
news.next = null;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if (p <= (fr).priority) {
news.next = fr;
(fr).prev = news.next;
fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if (p > (rr).priority) {
news.next = null;
(rr).next = news;
news.prev = (rr).next;
rr = news;
}
// Handle other cases
else {
// Find position where we need to
// insert.
Node start = (fr).next;
while (start.priority > p)
start = start.next;
(start.prev).next = news;
news.next = start.prev;
news.prev = (start.prev).next;
start.prev = news.next;
}
}
front = fr;
rear = rr;
}
// Return the value at rear
static int peek(Node fr) { return fr.info; }
static boolean isEmpty(Node fr) { return (fr == null); }
// Removes the element with the
// least priority value from the list
static int pop(Node fr, Node rr)
{
Node temp = fr;
int res = temp.info;
(fr) = (fr).next;
if (fr == null)
rr = null;
front = fr;
rear = rr;
return res;
}
// Driver code
public static void main(String args[])
{
push(front, rear, 2, 3);
push(front, rear, 3, 4);
push(front, rear, 4, 5);
push(front, rear, 5, 6);
push(front, rear, 6, 7);
push(front, rear, 1, 2);
System.out.println(pop(front, rear));
System.out.println(peek(front));
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Python3 code to implement priority
# queue using doubly linked list
# Linked List Node
class Node:
def __init__(self):
self.info = 0
self.priority = 0
self.next = None
self.prev = None
front = None
rear = None
# Function to insert a new Node
def push(fr, rr, n, p):
global front, rear
news = Node()
news.info = n
news.priority = p
# If linked list is empty
if (fr == None):
fr = news
rr = news
news.next = None
else:
# If p is less than or equal fr
# node's priority, then insert at
# the fr.
if (p <= (fr).priority):
news.next = fr
(fr).prev = news.next
fr = news
# If p is more rr node's priority,
# then insert after the rr.
elif (p > (rr).priority):
news.next = None
(rr).next = news
news.prev = (rr).next
rr = news
# Handle other cases
else:
# Find position where we need to
# insert.
start = (fr).next
while (start.priority > p):
start = start.next
(start.prev).next = news
news.next = start.prev
news.prev = (start.prev).next
start.prev = news.next
front = fr
rear = rr
# Return the value at rr
def peek(fr):
return fr.info
def isEmpty(fr):
return fr == None
# Removes the element with the
# least priority value from the list
def pop(fr, rr):
global front , rear
temp = fr
res = temp.info
(fr) = (fr).next
if (fr == None):
rr = None
front = fr
rear = rr
return res
# Driver code
if __name__=='__main__':
push( front, rear, 2, 3)
push( front, rear, 3, 4)
push( front, rear, 4, 5)
push( front, rear, 5, 6)
push( front, rear, 6, 7)
push( front, rear, 1, 2)
print(pop(front, rear))
print(peek(front))
# This code is contributed by rutvik_56
C#
// C# code to implement priority
// queue using doubly linked list
using System;
class GFG {
public static Node front, rear;
// Linked List Node
public class Node {
public int info;
public int priority;
public Node prev, next;
}
// Function to insert a new Node
public static void push(Node fr, Node rr, int n, int p)
{
Node news = new Node();
news.info = n;
news.priority = p;
// If linked list is empty
if (fr == null) {
fr = news;
rr = news;
news.next = null;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if (p <= (fr).priority) {
news.next = fr;
(fr).prev = news.next;
fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if (p > (rr).priority) {
news.next = null;
(rr).next = news;
news.prev = (rr).next;
rr = news;
}
// Handle other cases
else {
// Find position where we
// need to insert.
Node start = (fr).next;
while (start.priority > p) {
start = start.next;
}
(start.prev).next = news;
news.next = start.prev;
news.prev = (start.prev).next;
start.prev = news.next;
}
}
front = fr;
rear = rr;
}
// Return the value at rear
public static int peek(Node fr) { return fr.info; }
public static bool isEmpty(Node fr)
{
return (fr == null);
}
// Removes the element with the
// least priority value from the list
public static int pop(Node fr, Node rr)
{
Node temp = fr;
int res = temp.info;
(fr) = (fr).next;
if (fr == null) {
rr = null;
}
front = fr;
rear = rr;
return res;
}
// Driver code
public static void Main(string[] args)
{
push(front, rear, 2, 3);
push(front, rear, 3, 4);
push(front, rear, 4, 5);
push(front, rear, 5, 6);
push(front, rear, 6, 7);
push(front, rear, 1, 2);
Console.WriteLine(pop(front, rear));
Console.WriteLine(peek(front));
}
}
// This code is contributed by Shrikant13
JavaScript
<script>
// javascript code to implement priority
// queue using doubly linked list
var front, rear;
// Linked List Node
class Node {
constructor(){
this.info = 0;
this.priority = 0;
this.prev = null;
this.next = null;
}
}
// Function to insert a new Node
function push(fr, rr , n , p) {
var news = new Node();
news.info = n;
news.priority = p;
// If linked list is empty
if (fr == null) {
fr = news;
rr = news;
news.next = null;
} else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if (p <= (fr).priority) {
news.next = fr;
(fr).prev = news.next;
fr = news;
}
// If p is more rear node's priority,
// then insert after the rear.
else if (p > (rr).priority) {
news.next = null;
(rr).next = news;
news.prev = (rr).next;
rr = news;
}
// Handle other cases
else {
// Find position where we need to
// insert.
var start = (fr).next;
while (start.priority > p)
start = start.next;
(start.prev).next = news;
news.next = start.prev;
news.prev = (start.prev).next;
start.prev = news.next;
}
}
front = fr;
rear = rr;
}
// Return the value at rear
function peek(fr) {
return fr.info;
}
function isEmpty(fr) {
return (fr == null);
}
// Removes the element with the
// least priority value from the list
function pop(fr, rr) {
var temp = fr;
var res = temp.info;
(fr) = (fr).next;
if (fr == null)
rr = null;
front = fr;
rear = rr;
return res;
}
// Driver code
push(front, rear, 2, 3);
push(front, rear, 3, 4);
push(front, rear, 4, 5);
push(front, rear, 5, 6);
push(front, rear, 6, 7);
push(front, rear, 1, 2);
document.write(pop(front, rear)+"<br/>");
document.write(peek(front));
// This code contributed by aashish1995
</script>
Related Article :
Priority Queue using Singly Linked List
Time Complexities and Comparison with Binary Heap:
peek() push() pop()
-----------------------------------------
Linked List | O(1) O(n) O(1)
|
Binary Heap | O(1) O(Log n) O(Log n)
Space complexity :- O(N) The space complexity of the given code is O(n), where n is the number of elements in the priority queue. This is because we are using a doubly linked list to store the elements, and each element requires a node that contains its value, priority, and pointers to its previous and next nodes in the list. The space required for the linked list grows linearly with the number of elements. Additionally, we are using some constant extra space for temporary variables used in the push and pop operations.
Similar Reads
Priority Queue using Linked List
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 a
11 min read
Python | Queue using Doubly Linked List
A Queue is a collection of objects that are inserted and removed using First in First out Principle(FIFO). Insertion is done at the back(Rear) of the Queue and elements are accessed and deleted from first(Front) location in the queue. Queue Operations:1. enqueue() : Adds element to the back of Queue
3 min read
Reverse a Doubly linked list using recursion
Given a doubly linked list. Reverse it using recursion. Original Doubly linked list Reversed Doubly linked list We have discussed Iterative solution to reverse a Doubly Linked List Algorithm: If list is empty, return Reverse head by swapping head->prev and head->next If prev = NULL it means th
9 min read
Doubly Linked List using Sentinel Nodes
In the case of the simple doubly linked list, if we have to perform the insertion or deletion operation at the starting of the doubly linked list, end of the doubly linked list, or in between the starting and end nodes for each, we require to check different condition that makes the algorithm comple
15+ min read
Python | Stack using Doubly Linked List
A stack is a collection of objects that are inserted and removed using Last in First out Principle(LIFO). User can insert elements into the stack, and can only access or remove the recently inserted object on top of the stack. The main advantage of using LinkedList over array for implementing stack
4 min read
Double ended priority queue
A double ended priority queue supports operations of both max heap (a max priority queue) and min heap (a min priority queue). The following operations are expected from double ended priority queue. getMax() : Returns maximum element.getMin() : Returns minimum element.deleteMax() : Deletes maximum e
6 min read
Priority Queue using Binary Heap
What is a Priority Queue ?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 o
15+ min read
QuickSort on Doubly Linked List
Given a doubly linked list, the task is to sort the doubly linked list in non-decreasing order using the quicksort.Examples:Input: head: 5<->3<->4<->1<->2Output: 1<->2<->3<->4<->5Explanation: Doubly Linked List after sorting using quicksort technique i
12 min read
Reverse a Doubly Linked List
Given a Doubly Linked List, the task is to reverse the Doubly Linked List.Examples:Input: Doubly Linked List = 1 <-> 2 <-> 3 -> NULL Doubly Linked List Output: Reversed Doubly Linked List = 3 <-> 2 <-> 1 -> NULLReversed Doubly Linked ListInput: Doubly Linked List = 1 -
15 min read
Priority Queue Using Array
A priority queue is a data structure that stores elements with associated priorities. In a priority queue, elements are dequeued in order of their priority, with the highest priority elements being removed first. It is commonly used in algorithms like Dijkstra's for shortest path and in real-time sc
8 min read