Illustrated · 1 min

Queues

A queue is a first-in-first-out data structure. You can "enqueue" an element into the queue, which means it gets sent to the end of the queue, and you can "dequeue" an element from the queue, which means you obtain the data in the variable at the start of the queue and remove it from the queue. These are similar to adding and deleting elements from a list.

Queues also have a very appropriate name (if you're British, at least). When you wait in a line to go on a roller coaster, it only makes logical sense that the first person to enter the line (the first person enqueued) should go on the roller coaster first (dequeue). Unless you have one of those passes to cut to the front of the line, which brings me to another type of queue.

Priority Queues

A priority queue is a queue where each element has a "priority," which means each time an element is added, it is added to the queue according to its priority compared to all the other elements.

For example, in an emergency room, suppose a person enters with a stomach ache, another enters with a sore thumb, and a third enters with a broken arm. It might be a smarter idea to take the guy with the broken arm first, and then since the other two have ≈\approx the same priority, they can be sorted by the time they came in.

Type a concept, like “slope” or “photosynthesis”.

↑ ↓ to choose · Enter to openSearch explanations and problems