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.
1 FIFO rule
Enqueue 27, 14, 33 and the first dequeue hands back 27 — First In, First Out, no exceptions.
2 The circular trick
A ring buffer reuses freed slots: advance with (rear + 1) % N, so rear wraps 2 → 0 on a 3-slot queue.
3 Deque = both ends
std::deque offers push_front, push_back, pop_front and pop_back — every one of them O(1).
4 The BFS engine
Level-order graph search is a queue in disguise: the nearest unvisited nodes always leave first.
5 Where you meet it
Print spoolers, task schedulers, ticket lines and the sliding-window maximum all queue up.
Why queues win
FIFO rule1Enqueue 27, 14, 33 and the first dequeue hands back 27 — First In, First Out, no exceptions. The circular trick2A ring buffer reuses freed slots: advance with (rear + 1) % N, so rear wraps 2 → 0 on a 3-slot queue. Deque = both ends3std::deque offers push_front, push_back, pop_front and pop_back — every one of them O(1). The BFS engine4Level-order graph search is a queue in disguise: the nearest unvisited nodes always leave first. Where you meet it5Print spoolers, task schedulers, ticket lines and the sliding-window maximum all queue up. 5 building blocks make up Why queues win
One door in, one door out — watch jobs leave in exactly the order they arrived.
1 / 10An empty print queue — jobs arrive at the rear and leave from the front.
Enqueue on the left, dequeue on the right — both advance with the same % N formula.
process decision input / output start / end A circular array queue with wrap-around math — then a deque checking a palindrome from both ends.
globalq reserves three slots; front = 0, rear = -1, cnt = 0 — the classic empty ring.
Memory
q= [_, _, _]front= 0rear= -1cnt= 0
Console
output appears as the program runs…
Every window of 3 needs its maximum — a monotonic deque keeps only candidates that can still win.
1 / 15Find the max of every window of 3 in [4, 2, 8, 1, 6, 5]. The first window covers indexes 0..2.
std::queue gives you front, back and pop — trace each line.
Your predicted output — one line per console line
Hint: push adds at the back, pop removes from the front, and front/back only read — they never remove.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?FIFOdrops into place
?frontdrops into place
?reardrops into place
?cnt == Ndrops into place
?% N wrapdrops into place
?dequedrops into place
?BFS queuedrops into place
?monotonic dequedrops into place
01A queue removes from the front and inserts at the rear — strict FIFO.
02Circular arrays reuse freed slots: advance with (i + 1) % N.
03cnt == capacity is the full test; cnt == 0 is the empty test.
04std::deque gives O(1) operations at both ends — front and back together.
05BFS is a queue in disguise — the nearest unvisited nodes leave first.
06Sliding window maximum uses a monotonic deque to stay O(n).