9-Priority Queue Using DLL-09!01!2025
9-Priority Queue Using DLL-09!01!2025
URL:https://forms.gle/DshHXQBaA5wVptMK6
PRIORITY QUEUE USING DOUBLY LINKED LIST
EXPLANATION
list.
• The idea is to maintain a sorted linked list where elements are inserted
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:
pointers to null.
PRIORITY QUEUE USING DOUBLY LINKED LIST
EXPLANATION
3. Insertion Operation(PUSH()):
Start from the head and move to the right until you find a node with a
• Insert the new node at the correct position by updating the next and
EXPLANATION
4. Deletion Operation(POP()):
Example:
EXPLANATION
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
while (!priorityQueue.isEmpty()) {
System.out.println("Executing: " + priorityQueue.delete());
}
}
}
PRIORITY QUEUE USING DOUBLY LINKED LIST
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
3. `peek` operation: O(1) as it involves accessing the head element, which can be
operations.
PRIORITY QUEUE USING DOUBLY LINKED LIST
priority.
priority.
in the worst case because it may require traversing the entire list
O(1)).
INTERVIEW QUESTIONS
applications like:
graph.
https://
learn.codemithra.com