Implement Priority Queue using Linked Lists.
- 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.
Priority Queues can be implemented using common data structures like arrays, linked-lists, heaps and binary trees.
Prerequisites :
Linked Lists, Priority Queues
The list is so created so that the highest priority element is always at the head of the list. The list is arranged in descending order of elements based on their priority. This allow us to remove the highest priority element in O(1) time. To insert an element we must traverse the list and find the proper position to insert the node so that the overall order of the priority queue is maintained. This makes the push() operation takes O(N) time. The pop() and peek() operations are performed in constant time.
Algorithm :
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
Below is the implementation of the algorithm :
C++
#include <bits/stdc++.h>
using namespace std;
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;
}
int peek(Node** head)
{
return (*head)->data;
}
void pop(Node** head)
{
Node* temp = *head;
(*head) = (*head)->next;
free (temp);
}
void push(Node** head, int d, int p)
{
Node* start = (*head);
Node* temp = newNode(d, p);
if ((*head)->priority > p)
{
temp->next = *head;
(*head) = temp;
}
else
{
while (start->next != NULL &&
start->next->priority < p)
{
start = start->next;
}
temp->next = start->next;
start->next = temp;
}
}
int isEmpty(Node** head)
{
return (*head) == NULL;
}
int main()
{
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;
}
|
C
#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;
}
int peek(Node** head)
{
return (*head)->data;
}
void pop(Node** head)
{
Node* temp = *head;
(*head) = (*head)->next;
free (temp);
}
void push(Node** head, int d, int p)
{
Node* start = (*head);
Node* temp = newNode(d, p);
if ((*head)->priority > p) {
temp->next = *head;
(*head) = temp;
}
else {
while (start->next != NULL &&
start->next->priority < p) {
start = start->next;
}
temp->next = start->next;
start->next = temp;
}
}
int isEmpty(Node** head)
{
return (*head) == NULL;
}
int main()
{
Node* pq = newNode(4, 1);
push(&pq, 5, 2);
push(&pq, 6, 3);
push(&pq, 7, 0);
while (!isEmpty(&pq)) {
printf ( "%d " , peek(&pq));
pop(&pq);
}
return 0;
}
|
Java
import java.util.* ;
class Solution
{
static class Node {
int data;
int priority;
Node next;
}
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;
}
static int peek(Node head)
{
return (head).data;
}
static Node pop(Node head)
{
Node temp = head;
(head) = (head).next;
return head;
}
static Node push(Node head, int d, int p)
{
Node start = (head);
Node temp = newNode(d, p);
if ((head).priority > p) {
temp.next = head;
(head) = temp;
}
else {
while (start.next != null &&
start.next.priority < p) {
start = start.next;
}
temp.next = start.next;
start.next = temp;
}
return head;
}
static int isEmpty(Node head)
{
return ((head) == null )? 1 : 0 ;
}
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);
}
}
}
|
Python3
class PriorityQueueNode:
def __init__( self , value, pr):
self .data = value
self .priority = pr
self . next = None
class PriorityQueue:
def __init__( self ):
self .front = None
def isEmpty( self ):
return True if self .front = = None else False
def push( self , value, priority):
if self .isEmpty() = = True :
self .front = PriorityQueueNode(value,
priority)
return 1
else :
if self .front.priority > priority:
newNode = PriorityQueueNode(value,
priority)
newNode. next = self .front
self .front = newNode
return 1
else :
temp = self .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
def pop( self ):
if self .isEmpty() = = True :
return
else :
self .front = self .front. next
return 1
def peek( self ):
if self .isEmpty() = = True :
return
else :
return self .front.data
def traverse( self ):
if self .isEmpty() = = True :
return "Queue is Empty!"
else :
temp = self .front
while temp:
print (temp.data, end = " " )
temp = temp. next
if __name__ = = "__main__" :
pq = PriorityQueue()
pq.push( 4 , 1 )
pq.push( 5 , 2 )
pq.push( 6 , 3 )
pq.push( 7 , 0 )
pq.traverse()
pq.pop()
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public int priority;
public Node next;
}
public static Node node = new Node();
public static Node newNode( int d, int p)
{
Node temp = new Node();
temp.data = d;
temp.priority = p;
temp.next = null ;
return temp;
}
public static int peek(Node head)
{
return (head).data;
}
public static Node pop(Node head)
{
Node temp = head;
(head) = (head).next;
return head;
}
public static Node push(Node head,
int d, int p)
{
Node start = (head);
Node temp = newNode(d, p);
if ((head).priority > p)
{
temp.next = head;
(head) = temp;
}
else
{
while (start.next != null &&
start.next.priority < p)
{
start = start.next;
}
temp.next = start.next;
start.next = temp;
}
return head;
}
public static int isEmpty(Node head)
{
return ((head) == null ) ? 1 : 0;
}
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
<script>
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;
}
function peek(head) {
return head.data;
}
function pop(head) {
var temp = head;
head = head.next;
return head;
}
function push(head, d, p) {
var start = head;
var temp = newNode(d, p);
if (head.priority > p)
{
temp.next = head;
head = temp;
}
else
{
while (start.next != null && start.next.priority < p) {
start = start.next;
}
temp.next = start.next;
start.next = temp;
}
return head;
}
function isEmpty(head) {
return head == null ? 1 : 0;
}
var pq = newNode(4, 1);
pq = push(pq, 5, 2);
pq = push(pq, 6, 3);
pq = push(pq, 7, 0);
while (isEmpty(pq) == 0) {
document.write(peek(pq) + " " );
pq = pop(pq);
}
</script>
|
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Jan, 2023
Like Article
Save Article