0% found this document useful (0 votes)
5 views3 pages

Priority - Queue Using DLL

Uploaded by

suraj.k2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Priority - Queue Using DLL

Uploaded by

suraj.k2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

class Main {

// **Node Class**
// Represents each element in the priority queue
static class Node {
int data; // Value of the node
int priority; // Priority of the node
Node next, prev; // Pointers for doubly linked list

// Constructor to initialize the node


public Node(int data, int priority) {
this.data = data;
this.priority = priority;
}
}

// Head of the priority queue (doubly linked list)


private static Node head = null;

/**
* **Push Method**
* Inserts a new node in the queue based on its priority.
* @param data Value of the new node
* @param priority Priority of the new node
*/
private static void push(int data, int priority) {
// Case 1: If the queue is empty, make the new node the head
if (head == null) {
Node newNode = new Node(data, priority);
head = newNode;
return;
}

// Create a new node


Node node = new Node(data, priority);
Node temp = head, parent = null;

// Traverse to find the correct position based on priority


while (temp != null && temp.priority >= priority) {
parent = temp; // Track the previous node
temp = temp.next;
}

// Case 2: Insert at the beginning (highest priority)


if (parent == null) {
node.next = head;
head.prev = node;
head = node;
}
// Case 3: Insert at the end (lowest priority)
else if (temp == null) {
parent.next = node;
node.prev = parent;
}
// Case 4: Insert in between two nodes
else {
parent.next = node;
node.prev = parent;
node.next = temp;
temp.prev = node;
}
}

/**
* **Peek Method**
* Returns the value of the highest priority element without removing it.
* @return The data of the head node, or -1 if the queue is empty.
*/
private static int peek() {
if (head != null) {
return head.data; // Return the value of the head node
}
return -1; // If queue is empty
}

/**
* **Pop Method**
* Removes and returns the highest priority element from the queue.
* @return The data of the head node, or -1 if the queue is empty.
*/
private static int pop() {
if (head != null) {
int curr = head.data; // Store the value of the head node
head = head.next; // Move the head to the next node
if (head != null) {
head.prev = null; // Update the backward link
}
return curr; // Return the removed node's value
}
return -1; // If queue is empty
}

/**
* **Main Method**
* Demonstrates priority queue functionality with an example.
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Number of elements to insert


int n = sc.nextInt();

// Insert elements into the priority queue


for (int i = 0; i < n; i++) {
int data = sc.nextInt(); // Node value
int pri = sc.nextInt(); // Node priority
push(data, pri);
}

// Demonstrate priority queue operations


System.out.println(peek()); // Print the highest priority element
System.out.println(pop()); // Remove and print the highest priority
element
System.out.println(pop()); // Remove and print the next highest priority
element
System.out.println(peek()); // Print the new highest priority element
}
}

5
10 3
20 1
30 4
40 2
50 3

30 // Highest priority element (peek)


30 // Removed (pop)
10 // Removed (pop)
50 // New highest priority element (peek)

You might also like