Queues and Deques
Lesson 12 of 20Watch a circular queue wrap its buffer, a queue pump out binary numbers, and a deque work from both ends.
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.
FIFO in action
Printer jobs line up at the rear and leave from the front — never any cutting in line.
The printer queue starts empty — jobs will line up at the rear and be served from the front.
Circular queue flow
One array, two indices, modulo arithmetic — the buffer never shifts a single element.
Walkthrough
A circular buffer with wrap-around indexing — then binary numbers generated by a queue.
1public class CircularQueue {2public static void main(String[] args) {3String[] buf = new String[3];4int front = 0, rear = 0, size = 0;56buf[rear] = "A"; rear = (rear + 1) % 3; size++;7buf[rear] = "B"; rear = (rear + 1) % 3; size++;8System.out.println(buf[front] + " served");9front = (front + 1) % 3; size--;10buf[rear] = "C"; rear = (rear + 1) % 3; size++;11System.out.println(buf[front] + " served");12front = (front + 1) % 3; size--;13System.out.println(buf[front] + " served");14}15}
buf is a fixed 3-slot ring — no growing, no shifting.
Deque in motion
Same container, both doors open — ArrayDeque inserts and removes at either end in O(1).
A deque holding 20 and 30 — front at the left end, rear at the right end.
Predict the output
Queue mechanics — remove and peek always work at the front.
1import java.util.ArrayDeque;2import java.util.Queue;34public class Predict {5public static void main(String[] args) {6Queue<Integer> q = new ArrayDeque<>();7q.add(10);8q.add(20);9q.add(30);10System.out.println(q.remove());11System.out.println(q.peek());12System.out.println(q.size());13}14}
Hint: Queue add appends at the rear, remove takes from the front, peek only reads the front.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Queues are FIFO: offer at the rear, poll from the front — both O(1).
A circular queue reuses a fixed array with rear = (rear + 1) % cap.
size is the honest emptiness/fullness test — front == rear is ambiguous in a ring.
ArrayDeque is both a queue and a stack: offerFirst/offerLast/pollFirst/pollLast.
BFS owes its shortest-path guarantee to the queue behind it.
Sliding-window maximum uses a monotonic deque — each element enters and leaves once.