Queues and Deques

Lesson 12 of 20

Watch a print queue drain in strict FIFO order, then chase window maxima through an array with a deque.

The big picture

Why queues win

A queue is the patient sibling of the stack: values leave in exactly the order they arrived — and deques let both ends move at once.

FIFO rule

Enqueue 27, 14, 33 and the first dequeue hands back 27 — First In, First Out, no exceptions.

The circular trick

A ring buffer reuses freed slots: advance with (rear + 1) % N, so rear wraps 2 → 0 on a 3-slot queue.

Deque = both ends

std::deque offers push_front, push_back, pop_front and pop_back — every one of them O(1).

The BFS engine

Level-order graph search is a queue in disguise: the nearest unvisited nodes always leave first.

Where you meet it

Print spoolers, task schedulers, ticket lines and the sliding-window maximum all queue up.