0% found this document useful (0 votes)
32 views24 pages

9-Priority Queue Using DLL-09!01!2025

The document explains the implementation of a priority queue using a doubly linked list, detailing operations such as insertion, deletion, and retrieval of elements based on their priority. It outlines the time and space complexities associated with these operations, highlighting that insertion has a worst-case time complexity of O(n) while deletion and retrieval are O(1). Additionally, it addresses potential interview questions related to priority queues and their applications in real-world scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views24 pages

9-Priority Queue Using DLL-09!01!2025

The document explains the implementation of a priority queue using a doubly linked list, detailing operations such as insertion, deletion, and retrieval of elements based on their priority. It outlines the time and space complexities associated with these operations, highlighting that insertion has a worst-case time complexity of O(n) while deletion and retrieval are O(1). Additionally, it addresses potential interview questions related to priority queues and their applications in real-world scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PRIORITY QUEUE USING

DOUBLY LINKED LIST


TEST TIME ON THE CELEBRITY PROBLEM

URL:https://forms.gle/DshHXQBaA5wVptMK6
PRIORITY QUEUE USING DOUBLY LINKED LIST

EXPLANATION

• A priority queue is a data structure that stores elements with

associated priorities and allows efficient retrieval of the element with

the highest priority.

• In this implementation, we'll create a priority queue using a linked

list.

• The idea is to maintain a sorted linked list where elements are inserted

in such a way that higher priority elements come first.


PRIORITY QUEUE USING DOUBLY LINKED LIST

EXPLANATION

1. Node Definition:

• Create a doubly linked list node that holds both the data and the

priority. Each node will have references to the previous and next nodes

in the list.

2. Initialization:

• Initialize an empty priority queue by setting its head and tail

pointers to null.
PRIORITY QUEUE USING DOUBLY LINKED LIST

EXPLANATION

3. Insertion Operation(PUSH()):

• When inserting an element with a priority, create a new node with

the data and priority.

• Traverse the list to find the correct position based on priority.

Start from the head and move to the right until you find a node with a

lower or equal priority.

• Insert the new node at the correct position by updating the next and

previous pointers accordingly.


PRIORITY QUEUE USING DOUBLY LINKED LIST

EXPLANATION

4. Deletion Operation(POP()):

• To remove the highestpriority element, simply remove the node at the

head of the list.

• Update the head pointer to point to the next node.

5. Retrieval Operation(PEEK() or TOP()):

• To retrieve the highestpriority element without removing it, access

the data of the node at the head of the list.


PRIORITY QUEUE USING DOUBLY LINKED LIST
EXPLANATION

Example:

Suppose you have a priority queue of tasks with priorities:

❖ Task A with priority 3

❖ Task B with priority 1

❖ Task C with priority 2

After inserting these tasks in order, your doubly linked list


representation of the priority queue would look like this:

[Task B, priority: 1] <> [Task C, priority: 2] <> [Task A,


priority: 3]
The highest priority task is Task B with priority 1.
PRIORITY QUEUE USING DOUBLY LINKED LIST

EXPLANATION

A priority queue based on age would

prioritize older people first, ensuring

that they are served or processed before

younger individuals. This is useful in

scenarios like healthcare triage, where

elderly patients might need immediate

attention.
PRIORITY QUEUE USING DOUBLY LINKED LIST
SAMPLE 1:INPUT
OUTPUT
Sample Input 1:
Explanation:
priorityQueue.insert("Task A", 3);
In this example, tasks with
priorityQueue.insert("Task B", 1);
different priorities are inserted
priorityQueue.insert("Task C", 2);
into the priority queue.
Sample Output 1:
The highest-priority task "Task B"
Highest-priority task: Task B
is retrieved and executed first,
Executing: Task B
followed by "Task C" and "Task A"
Executing: Task C
in order of their priorities.
Executing: Task A
PRIORITY QUEUE USING DOUBLY LINKED LIST
SAMPLE 2:INPUT
OUTPUT
Sample Input 2:
Explanation:
priorityQueue.insert("Job X", 2);
In this example, jobs with
priorityQueue.insert("Job Y", 4);
different priorities are inserted.
priorityQueue.insert("Job Z", 1);
"Job Z" has the highest priority,
Sample Output 2:
so it is executed first, followed
Highest-priority task: Job Z
by "Job X" and "Job Y" in order of
Executing: Job Z
their priorities.
Executing: Job X

Executing: Job Y
PRIORITY QUEUE USING DOUBLY LINKED LIST

class PriorityQueueNode<T> { public void insert(T data, int


T data; priority) {
int priority; PriorityQueueNode<T> newNode =
PriorityQueueNode<T> prev; new PriorityQueueNode<>(data, priority);
PriorityQueueNode<T> next; if (head == null) {
head = newNode;
public PriorityQueueNode(T data, int tail = newNode;
priority) { }
this.data = data; else if (priority <
this.priority = priority; head.priority) {
} newNode.next = head;
} head.prev = newNode;
head = newNode;
class PriorityQueue<T> { } else {
private PriorityQueueNode<T> head; PriorityQueueNode<T> current =
private PriorityQueueNode<T> tail; head;
public PriorityQueue() { while (current.next != null &&
head = null; current.next.priority <= priority) {
tail = null; current = current.next;
} }
PRIORITY QUEUE USING DOUBLY LINKED LIST

newNode.prev = current; head.prev = null;


newNode.next = current.next; } else {
tail = null;
if (current.next != null) { }
current.next.prev =
newNode; return data;
} else { }
tail = newNode; public T peek() {
} if (head == null) {
return null;
current.next = newNode; }
} return head.data;
} }

public T delete() { public boolean isEmpty() {


if (head == null) { return head == null;
return null; }}
}
T data = head.data;
head = head.next;
if (head != null) {
PRIORITY QUEUE USING DOUBLY LINKED LIST

public class Main {


public static void main(String[] args) {
PriorityQueue<String> priorityQueue = new PriorityQueue<>();

priorityQueue.insert("Task A", 3);


priorityQueue.insert("Task B", 1);
priorityQueue.insert("Task C", 2);

System.out.println("Highest-priority task: " + priorityQueue.peek());

while (!priorityQueue.isEmpty()) {
System.out.println("Executing: " + priorityQueue.delete());
}
}
}
PRIORITY QUEUE USING DOUBLY LINKED LIST

TIME COMPLEXITY AND SPACE COMPLEXITY

1. `insert` operation: O(n) in the worst case, where n is the number of elements

in the priority queue. This is because, in the worst case, the algorithm may need

to traverse the entire list to find the correct position for insertion.

2. `delete` operation: O(1) as it involves removing the head element, which can

be done in constant time.

3. `peek` operation: O(1) as it involves accessing the head element, which can be

done in constant time.


PRIORITY QUEUE USING DOUBLY LINKED LIST

TIME COMPLEXITY AND SPACE COMPLEXITY

The time complexity of each individual operation is determined

by the most time-consuming operation, which is the `insert`

operation in this case. Therefore, the overall time complexity

of the code is O(n) in the worst case for a sequence of `insert`

operations.
PRIORITY QUEUE USING DOUBLY LINKED LIST

TIME COMPLEXITY AND SPACE COMPLEXITY

The Space Complexity:

O(n) for storing the elements in the linked list.


INTERVIEW QUESTIONS

1. What is a Priority Queue?

Answer: A Priority Queue is a data structure that stores

elements with associated priorities and allows efficient retrieval

of the element with the highest priority.


INTERVIEW QUESTIONS

2. Explain the main operations of a PriorityQueue.

Answer: The main operations of a PriorityQueue include:

`insert(data, priority)`: Inserts an element with a given

priority.

`delete()`: Removes and returns the element with the highest

priority.

`peek()`: Returns the element with the highest priority

without removing it.


INTERVIEW QUESTIONS

3. Why would you choose a Doubly Linked List to implement a


Priority Queue?

Answer: A Doubly Linked List is chosen because it allows for

efficient insertion and removal of elements with different

priorities while maintaining the order based on priority. This is

crucial for PriorityQueue operations.


INTERVIEW QUESTIONS

4. What is the time complexity of the `insert` operation in a


Priority Queue implemented with a Doubly Linked List?

Answer: The time complexity of the `insert` operation is O(n)

in the worst case because it may require traversing the entire list

to find the correct position for insertion.


INTERVIEW QUESTIONS

5. How can you improve the time complexity of the `insert`


operation in a Priority Queue?

``Answer: You can improve the time complexity by using data

structures like Heaps (e.g., Binary Heap or Fibonacci Heap), which

offer better time complexity for insertion (O(log n) or amortized

O(1)).
INTERVIEW QUESTIONS

6. What are some real-world applications where a PriorityQueue with

Doubly Linked List can be useful?

Answer: Priority Queue with Doubly Linked List can be useful in

applications like:

⮚ Task scheduling in operating systems, where processes with

higher priority need to be executed first.

⮚ Dijkstra's algorithm for finding the shortest path in a

graph.

⮚ Network traffic management, where packets with higher


/ Ethnus /ethnus /
ethnuscodemith Codemithra code_mithra
ra

https://
learn.codemithra.com

[email protected] +91 7815 095 +91 9019 921


om 095 340

You might also like