Implementation of Priority Queue using Linked List in Java

Implementation of Priority Queue using Linked List in Java

Normal queue follows a First In First Out (FIFO) order to insert and remove an element. However, in a priority queue, an item with the highest priority comes out first. On this page we will discuss priority queue implementation using linked list.

Priority Queue Implementation using Array

Every element in the priority queue is associated with a priority. It does not matter in which order we insert the items in the queue, the item with higher priority must be removed before the item with the lower priority. If the two items have same priorities, the order of removal is not defined and depends on implementation.

OPERATIONS IN PRIORITY QUEUE :

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

ALGORITHM:

For Insertion :
  • START
  • USE FUNCTION INSERT
static Node insert(Node head, int d, int p) 
{ 
  Node start = (head); 
  
  // Create new Node 
  Node tp = newNode(d, p); 
  
  
  if ((head).prioritydata > p) { 
  
    // Insert New Node before head 
    tp.link = head; 
    (head) = tp; 
  } 
  else { 
  
    while (start.link != null && 
      start.link.prioritydata < p) { 
      start = start.link; 
    } 
  
    tp.link = start.link; 
    start.link = tp; 
  } 
  return head; 
}
  • END
For Deletion :
  • START
  • USE FUNCTION DELETE
static Node delete(Node head) 
{ 
  Node tp = head; 
  (head) = (head).link; 
  return head; 
}
  • END

Java code to implement priority queue using queue class 

Run

import java.util.*;
class Main
{
  static class Node
  {
    int data;
    int prioritydata;
    Node next;
  }
  static Node node = new Node ();
  static Node newNode (int d, int p)
  {
    Node tp = new Node ();
      tp.data = d;
      tp.prioritydata = p;
      tp.next = null;
      return tp;
  }

// Return the value at head

  static int peek (Node head)
  {

    return (head).data;

  }
//delete the element with the high priority

  static Node delete (Node head)
  {
    Node tp = head;
    (head) = (head).next;
    return head;

  }

// Function to insert element

  static Node insert (Node head, int d, int p)
  {
    Node start = (head);

    // Create new Node
    Node tp = newNode (d, p);
    if ((head).prioritydata > p)
      {

	// Insert New Node before head
	tp.next = head;
	(head) = tp;
      }

    else
      {
	while (start.next != null && start.next.prioritydata < p)
	  {
	    start = start.next;
	  }

	tp.next = start.next;
	start.next = tp;
      }
    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[])
  {
    // Create a Priority Queue
    // 20 and 40 and 60 and 80

    Node pq = newNode (40, 1);
    pq = insert (pq, 80, 2);
    pq = insert (pq, 60, 3);
    pq = insert (pq, 20, 0);

    while (isEmpty (pq) == 0)
      {
	System.out.println (peek (pq));
	pq = delete (pq);
      }
  }
}
Output :
20
40
80
60

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java