In the real world, while we might say we’re using a queue, there are those who can, and will, skip ahead of members already in the queue. Now, I’m not talking about skipping ahead of a person, like a line skipper in elementary school.
A simple example you might think of using a queue, is a walk in clinic medical clinic, where there are no reservations. All things being equal, the doctor sees you in the order you come into the clinic. However, patients who are more severe will be skipped ahead of less severe cases.
Consider a situation where you have 20 patients, and someone comes in who is having a heart attack. That person gets moved up the list so they are worked on immediately, sometimes pulling a doctor and/or nurse(s) away from a patient(s) they are currently examining.
So the nurse who checks you into a medical facility does a quick review of your situation and determines if you should be added to the end of the queue, or if you should be moved forward based upon your condition(s). To maintain efficiency, they may have some basic groups (high, medium, and low priority for example), but this allows them to work on severe cases first.
This is the perfect example of a priority queue. When an element is added to the queue, a check is done for the priority. This priority value is based upon whatever criteria is determined by the developer(s). The new item moves up the list until the new element’s priority is not greater than the existing item’s priority, or it reaches the top of the list.
This, of course, does mean that a low priority item may never be processed and used.
Priority queues are not just seen in medical facilities, but in many facets of life. Whether it’s at a restaurant where someone gets seated first because they know the owner, or the CEO’s computer getting fixed first because they are the CEO, even if they don’t use it nearly as much, and are out of the office.
Knowing how to properly implement a priority queue is very important, and it often takes some tweaking of the queue class and methods.
In one instance I was tasked with building a queue system for the internal helpdesk. While I could have just put all the tickets into a basic queue, not all tickets are created equal. Minor issues, like a printer being low on toner, shouldn’t require the same response rate as an external system which affected our customers being down.
So we built a priority queue system based upon the severity of the issue, combined with how many people were affected. Each of the two questions we asked had five possible answers, from a minor inconvenience that is only affecting me, to an external facing server being down. We worked to combine these values to determine where the items were put into the queue.
In a priority queue, your dequeue method would naturally work the same way. However, the enqueue method would check the priority, and move up the queue until the next queue element has a higher or same priority. That means, if the priority is the same, then it behaves like a regular queue – you only skip elements if your priority is higher.
Priority Queues was originally found on Access 2 Learn