Priority Queue using Doubly Linked List
Given Nodes with their priority, implement a priority queue using doubly linked list.
Prerequisite : Priority Queue
- push(): This function is used to insert a new data into the queue.
- pop(): This function removes the element with the lowest priority value from the queue.
- peek() / top(): This function is used to get the lowest priority element in the queue without removing it from the queue.
1. Create a doubly linked list having fields info(hold the information of the Node), priority(hold the priority of the Node), prev(point to previous Node), next(point to next Node).
2. Insert the element and priority in the Node.
3. Arrange the Nodes in the increasing order of priority.
Below is the implementation of above steps :
Related Article :
Priority Queue using Singly Linked List
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)
Space complexity :- O(N) The space complexity of the given code is O(n), where n is the number of elements in the priority queue. This is because we are using a doubly linked list to store the elements, and each element requires a node that contains its value, priority, and pointers to its previous and next nodes in the list. The space required for the linked list grows linearly with the number of elements. Additionally, we are using some constant extra space for temporary variables used in the push and pop operations.
Please Login to comment...