Queues and Deques

Lesson 12 of 20

Watch a circular queue wrap its buffer, a queue pump out binary numbers, and a deque work from both ends.

The big picture

Meet the queue

A queue is the polite data structure: first come, first served. Add at the rear, serve from the front — and the deque variant lets you work at both ends at once.

FIFO rule

First In, First Out. Offer A, B, C and the first poll returns A — exactly the order they joined.

Two O(1) ends

Enqueue lands at the rear, dequeue leaves from the front. Neither ever shifts the other elements.

Circular buffer

A fixed array recycles slots with modulo math: rear = (rear + 1) % cap wraps back to 0 at the wall.

Deque = both ends

ArrayDeque also offers addFirst, addLast, pollFirst and pollLast — a queue and a stack in one class.

Where you meet it

Printer spoolers, BFS frontiers, ticket lines and sliding-window maximums (a monotonic deque) all queue up.