Data Structures ・ O(1)

A container where the first value enqueued is the first one dequeued.

Move it step by step

Instruction list (runs left to right)

  1. enqueue 3
  2. enqueue 7
  3. enqueue 5
  4. dequeue
  5. enqueue 9
  6. dequeue
  7. dequeue
  8. dequeue
Leaves from the frontJoins at the back
Value out
  • just pushed/enqueued
  • value out
  • empty slot
  • error
  • front is the head, rear is the last position

Step 1 of 9Instructions run: 0Values inside: 0

Running 8 instructions, one by one, on an empty queue. It holds up to 5 values.

The pseudocode line currently running

  1. 1enqueue(x)
  2. 2if full, error (overflow)
  3. 3place x at the back, and make that spot rear
  4. 4dequeue()
  5. 5if empty, error (nothing to return)
  6. 6return the value at front, then make the next value front

Change the example

Try predicting

You're on step 1.
What comes out on the next dequeue?

What you'll learn

  • The flow where the first value in is the first one out
  • When each of the two markers, front and rear, moves

Prerequisites

  • Arrays and indices (numbering that starts at 0)
  • A stack's push and pop

How a queue works

A queue is a container where you join values to the back of a line, and take them out from the front of the line. The value that went in first comes out first, so it's called first-in-first-out (FIFO).

Joining the line is called enqueue, and taking a value out is called dequeue. The position at the head is called front, and the position at the tail is called rear.

Steps, with an example

  1. 1For enqueue, check that it isn't full, then place the value behind rear
  2. 2For dequeue, check that it isn't empty, then return the value at front
  3. 3After returning it, make the next value the new front
  4. 4Treat a dequeue when empty, and an enqueue when full, as errors
When you run the "Typical example" instructions in order
InstructionValue outContents (front to back)
enqueue 3None[3]
enqueue 7None[3, 7]
enqueue 5None[3, 7, 5]
dequeue3[7, 5]
enqueue 9None[7, 5, 9]
dequeue7[5, 9]
dequeue5[9]
dequeue9[]

Cost and complexity

OperationCostComplexity
enqueue1 step (just place it at the back)O(1)
dequeue1 step (just move front forward by one)O(1)
Removing the front of an array directlyn steps (shift the rest one spot to the left)O(n)

For clarity, the figure shifts the remaining values left after one comes out. A real queue doesn't shift anything; it just moves the front position forward by one, so dequeue only costs a single step.

When to use it

Good fit

  • When you want to process things in the order they arrived (a print queue, taking orders)
  • When you want to explore nearby things first (breadth-first search)

Not a good fit

  • When you want to process the most recent item first (use a stack instead)
  • Repeatedly calling pop(0) on a Python list (it's slow because it shifts everything each time; use deque instead)

Code

Pseudocode

  1. 1enqueue(x)
  2. 2if full, error (overflow)
  3. 3place x at the back, and make that spot rear
  4. 4dequeue()
  5. 5if empty, error (nothing to return)
  6. 6return the value at front, then make the next value front

Working code

pythonRunnable
Output

▶ Press “Run” to see the result here

Edit the Python or JavaScript and press “Run” to see the result right away. Java is read-only here.

FAQ

What's the difference between a stack and a queue?
They're filled the same way; they differ in which end values come out from. A stack comes out from the last value in, a queue from the first.
Why is list.pop(0) slow in Python?
Because after removing the front item, everything else has to shift one spot to the left. collections.deque's popleft doesn't shift anything, so the cost stays the same no matter how many items there are.

Related