Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Show how to implement a first-in, first-out (FIFO) queue with a priority queue.

ID: 3765650 • Letter: S

Question

Show how to implement a first-in, first-out (FIFO) queue with a priority queue. Provide pseudocode for the two common queue operations: enqueue and dequeue, with error checking. There should be a limit on the number of elements the queue can contain and the queue should allow new elements be enqueued as long as it is not full. Also, you should use as few additional bits as possible on each element in the queue to implement the FIFO mechanism. Analyze the running time of your code and explain why.

Explanation / Answer

FIFO queuewhen an element is inserted, it is given a value according to the inserting order. The later the element is inserted, the larger the value is given. Then we can implement a FIFO queue with a min-priority queue.
STACKJust like FIFO queue, when an element is inserted, it is given a value according to the inserting order. Then we can implement a stack with a max-priority queue.

Another Better way:

>For a stack we keep adding elements in increasing priority, while in a queue we add them in decreasing priority. For the stack we can set the new priority to HEAP-MAXIMUM(A) + 1. For the queue we need to keep track of it and decrease it on every insertion.
>Both are not very efficient. Furthermore, if the priority can overflow or underflow, so will eventually need to reassign priorities.