Search code examples
queuepriority-queue

Please describe how to use a priority queue to implement a queue


I don't understand this question....

Please describe how to use a priority queue to implement a queue.

Do I simply assign the priority as the time of entrance? and since a queue is fifo I would min prioritize so the oldest time comes first?


Solution

  • Using the time as the priority key is one way to do it. Be careful, though, to use a time that doesn't change externally. You wouldn't want to be using local time when it comes time to set your clocks back an hour during the Daylight Saving Time switch.

    You could also start an integer counter at 0, and increment it with every item you add to the queue.

    In theory, you could just give every item equal priority, but in practice that might end up acting like a stack. It depends on how your priority queue implementation treats equal items. If the implementation is a binary heap, for example, it could insert the equal item as the new smallest item. So you'd end up with LIFO.