A queue is a line. New items join at one end, the back, and items leave from the other end, the front — in exactly the order they arrived. Whoever has waited longest is served next. That rule has a name: FIFO — first in, first out.
Nobody pushes in at the front, and nobody is served from the middle. You join at the back and wait your turn. A queue in programming follows the same rules, with five operations:
Each one is O(1): enqueue only ever touches the back, and dequeue only the front, however long the line gets.
Free account
Sign up to read the rest of this lesson: 6 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come
Was this helpful?