Two queues are needed. One queue is used to store the data elements, and another is used for storing priorities.
To implement a priority queue using queues, you would typically need at least two queues. One queue would hold the elements of the priority queue, and the other queue would be used temporarily for reordering elements based on their priority.
Here’s a simple approach:
- Main Queue: This queue holds the actual elements of the priority queue.
- Temporary Queue: This queue is used for reordering elements when inserting a new element into the priority queue.
When inserting a new element into the priority queue:
- If the priority of the new element is higher than the priority of the front element of the main queue, then dequeue elements from the main queue and enqueue them into the temporary queue until you find the correct position for the new element.
- Enqueue the new element into the main queue.
- Enqueue the elements from the temporary queue back into the main queue.
So, the answer is at least two queues are required to implement a priority queue.