Queues
FIFO — First In, First OutA queue works like a line at a ticket counter: people join at the rear and are served from the front. Try the linear queue first — then switch to the circular queue and see how it reuses the empty space.
- Enqueuejoin at the rear
- Dequeueleave from the front
- Circulari = (i + 1) % CAPACITY
- Current
- New / found
- Leaving
- Wasted
- Empty
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
How is a queue stored?
An array plus two indexes: front (the next item to leave) and rear (the last item that joined). An empty queue has front = rear = −1. Items join at the rear and leave from the front.
#define CAPACITY 8 int queue[CAPACITY]; int front = -1; // index of the first item int rear = -1; // index of the last item // Circular queue: move with i = (i + 1) % CAPACITY
Enqueue
- Check the queue is not full.
- If it was empty, set front = 0.
- Move rear forward by one.
- Store the value at queue[rear]. O(1).
Dequeue
- Check the queue is not empty.
- Read the value at queue[front].
- Move front forward (or reset to −1 if it was the last item).
- Return the value. O(1).
The linear queue problem
- front only ever moves right.
- Slots before front are never reused.
- When rear reaches the end, the queue says "full"…
- …even though the start of the array is empty.
Circular queue: wrap around
- Treat the array as a ring.
- Move with i = (i + 1) % CAPACITY.
- After the last index comes index 0 again.
- Full when (rear + 1) % CAPACITY == front.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of items. Every queue operation works on front or rear only, so none of them depends on n.
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Enqueue | O(1) |
O(1) |
O(1) |
Instant |
| Dequeue | O(1) |
O(1) |
O(1) |
Instant |
| Front / Rear | O(1) |
O(1) |
O(1) |
Instant |
| isEmpty / isFull | O(1) |
O(1) |
O(1) |
Instant |
| Search for a value | O(1) |
O(n) |
O(n) |
Grows with n |
Linear queue vs circular queue
| Linear queue | Circular queue | |
|---|---|---|
| Moving forward | i = i + 1 | i = (i + 1) % CAPACITY |
| Is full when | rear == CAPACITY − 1 | (rear + 1) % CAPACITY == front |
| Space before front | Wasted — never reused | Reused when rear wraps around |
| Can say "full" with empty slots? | Yes | No |
Stack vs queue
| Stack | Queue | |
|---|---|---|
| Order | LIFO — last in, first out | FIFO — first in, first out |
| Add | push (at top) | enqueue (at rear) |
| Remove | pop (from top) | dequeue (from front) |
| Pointers | top | front and rear |
| Real-life example | Pile of plates | Line at a ticket counter |
| Used for | Undo, function calls, brackets | Printing jobs, task scheduling, BFS |
Check your understanding
Answer each question — you will see the explanation straight away.
You enqueue 10, then 20, then 30. What does dequeue() return?
First In, First Out: 10 joined first, so it leaves first.
In a queue, new items are added at the … and removed from the …
Like a line of people: you join at the back (rear) and are served at the front.
A linear queue of capacity 8 has front = 5 and rear = 7. Why can it not accept another item?
Only 3 items are in the queue, but a linear queue cannot move rear back to the start — the space before front is wasted.
In a circular queue of capacity 8, rear = 7. Where does the next item go?
The modulo makes the index wrap around to 0 (as long as index 0 is free).
When is a circular queue full?
If moving rear one step forward would land on front, every slot is in use.
Queues quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.