Skip to content

Queues

First in, first out. Add at the back, remove from the front, the fairness structure behind print jobs, ticket systems and breadth-first search.

Class 11 to 12beginner20 min2 lessons1 interactive lab
By the end you will be able to
  • State the FIFO rule and contrast it with LIFO
  • Perform enqueue and dequeue
  • Explain why BFS needs a queue rather than a stack

Finish first

Cost at a glance

enqueue
O(1)
dequeue
O(1)
peek
O(1)
search
O(n)

Lesson 1 of 2

Two ends, one direction

A queue uses both ends but each for one job only: enqueue adds at the back, dequeue removes from the front. That is FIFO, first in, first out, and it is the structure of fairness. Whoever waited longest goes next.

The distinction from a stack is not academic. Breadth-first search explores a graph level by level precisely because it uses a queue; swap in a stack and the same code becomes depth-first search, diving down one branch instead. Same algorithm skeleton, different structure, completely different traversal order.

Interactive lab

Queue operations

Enqueue and dequeue with the front and back markers tracked at every step.

Queue: first in, first out

enqueue / dequeue O(1)
frontback
4
8
1 / 2

Start

Peek reads the front item without removing it.

Continue

Lesson 2 of 2

The circular queue

The obvious array implementation is wrong in an interesting way. If dequeue removes index 0 and shifts everything left, the operation is O(n) and the queue's main selling point is gone. Move a front index forward instead and dequeue becomes O(1), but now the space at the start of the array is abandoned: after a few thousand operations the queue occupies the far end of a mostly empty block.

A circular queue fixes this by letting both indexes wrap around with modulo arithmetic. When back reaches the end of the array it continues at index 0, reusing the slots that dequeue freed. The array is never resized and nothing ever shifts, which is why fixed-capacity ring buffers are the standard choice in audio pipelines, network drivers and any embedded system where allocation during operation is unacceptable.

One detail is worth knowing before you implement one. When front equals back, the queue is either completely empty or completely full, and the indexes alone cannot tell you which. Implementations resolve this by tracking a count, or by deliberately leaving one slot empty so that full and empty look different.

Continue

Worked examples

Read the code, then change it

Copy any example into the playground and break it on purpose. That is the fastest way to learn what each line is holding up.

A queue without shiftingJavaScript
class Queue {
  constructor() {
    this.items = new Map();
    this.front = 0;
    this.back = 0;
  }
  enqueue(value) {
    this.items.set(this.back++, value);
  }
  dequeue() {
    if (this.front === this.back) return undefined;
    const value = this.items.get(this.front);
    this.items.delete(this.front++);
    return value; // O(1) -- no elements shift
  }
}

const q = new Queue();
q.enqueue('a'); q.enqueue('b');
console.log(q.dequeue(), q.dequeue());

Practice

Work these out yourself

No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.

0/3 attempted

Assessment

Check your understanding

Answer each question, then read the explanation. That is where the learning is.

0/5
  1. Question 1: Enqueue 4, 8, 12 then dequeue twice. What remains at the front?
    Question 1 / 5

    Enqueue 4, 8, 12 then dequeue twice. What remains at the front?

    Select an option first
  2. Question 2: Replacing the queue in breadth-first search with a stack turns it into:
    Question 2 / 5

    Replacing the queue in breadth-first search with a stack turns it into:

    Select an option first
  3. Question 3: An array-backed queue dequeues by removing index 0 and shifting everything left. What does dequeue cost?
    Question 3 / 5

    An array-backed queue dequeues by removing index 0 and shifting everything left. What does dequeue cost?

    Select an option first
  4. Question 4: What problem does a circular queue solve?
    Question 4 / 5

    What problem does a circular queue solve?

    Select an option first
  5. Question 5: In a circular queue, front and back are equal. What does that tell you?
    Question 5 / 5

    In a circular queue, front and back are equal. What does that tell you?

    Select an option first
5 of 5 questions left.

Finished this topic?

0 of 2 lessons marked done. Marking the topic complete ticks the rest.