Queue Ready
size 3 / 8
front 0
rear 2
status OK
  • Current
  • New / found
  • Leaving
  • Wasted
  • Empty
← leaves from the front (dequeue) joins at the rear (enqueue) ←
front
rear

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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

  1. Check the queue is not full.
  2. If it was empty, set front = 0.
  3. Move rear forward by one.
  4. Store the value at queue[rear]. O(1).

Dequeue

  1. Check the queue is not empty.
  2. Read the value at queue[front].
  3. Move front forward (or reset to −1 if it was the last item).
  4. Return the value. O(1).

The linear queue problem

  1. front only ever moves right.
  2. Slots before front are never reused.
  3. When rear reaches the end, the queue says "full"…
  4. …even though the start of the array is empty.

Circular queue: wrap around

  1. Treat the array as a ring.
  2. Move with i = (i + 1) % CAPACITY.
  3. After the last index comes index 0 again.
  4. Full when (rear + 1) % CAPACITY == front.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 ENQUEUE(queue, value)
2 if rear = CAPACITY − 1 then report "Overflow"; stop
3 if front = −1 then front ← 0
4 rear ← rear + 1
5 queue[rear] ← value
Complexity

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
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

You enqueue 10, then 20, then 30. What does dequeue() return?

Question 2

In a queue, new items are added at the … and removed from the …

Question 3

A linear queue of capacity 8 has front = 5 and rear = 7. Why can it not accept another item?

Question 4

In a circular queue of capacity 8, rear = 7. Where does the next item go?

Question 5

When is a circular queue full?

Assessment

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.