Queue (FIFO) — Data Structures

A queue is a FIFO data structure (First In, First Out): the first item in is the first one out, like a checkout line. Enqueue puts an item at the back…

Five people send documents to the office printer at almost the same time. The printer doesn't pick the shortest document or the newest one. It prints in the order the documents arrived: the document sent first comes out first.

This order is the opposite of a stack. The data structure is called a queue: new items join at the back, and items are taken from the front. Its rule is FIFO, short for First In, First Out: the first item in is the first one out. A queue works like the line at a checkout: the person who arrives first is served first.

A queue from a plain array

Index 0 is the front of the queue, and the end of the array is the back. Joining the queue is called enqueue, and leaving it is called dequeue. The array queue starts as ["A", "B", "C"], so A is at the front:

Step Code Result Queue (front to back)
1 queue.push("D") D joins at the back A, B, C, D
2 queue[0] "A", only looked at A, B, C, D
3 queue.shift() "A" B, C, D
4 queue.shift() "B" C, D

This is the code:

const queue = ["A", "B", "C"]; // front to back

queue.push("D");            // enqueue: ["A", "B", "C", "D"]
console.log(queue[0]);      // A (front: only looked at)
console.log(queue.shift()); // A (dequeue)
console.log(queue.shift()); // B
console.log(queue);         // ["C", "D"]

The items leave in the same order they joined: A, then B.

Why not shift()

Guess first: a queue of 1,000 people is stored in an array, and the person at the front is served with shift(). How many people have to move: 0, 1 or 999?

  1. An array can't have gaps. So when the item at index 0 is taken, every other item moves forward one index: the other 999 people all move. That's why shift() is O(n): the number of steps grows with the number of items.

The fix: don't move the people, move the markers instead. A marker is a variable that holds a number. The items are kept in an object called items with numbers as its keys, like a row of numbered boxes. There are two markers: head is the number of the front item, and tail is the number where the next item goes. This table records three enqueues, then one dequeue:

Step Code items head tail
1 enqueue("A") {0: "A"} 0 1
2 enqueue("B") {0: "A", 1: "B"} 0 2
3 enqueue("C") {0: "A", 1: "B", 2: "C"} 0 3
4 dequeue() gives "A" {1: "B", 2: "C"} 1 3

This is a Queue class that uses items, head and tail. When a queue is made, constructor sets this.items to an empty object, and this.head and this.tail to 0.

class Queue {
  constructor() {
    this.items = {};
    this.head = 0; // number of the front item
    this.tail = 0; // number where the next item goes
  }

  enqueue(item) {
    this.items[this.tail] = item;
    this.tail++;
  }

  dequeue() {
    if (this.head === this.tail) return undefined; // empty
    const item = this.items[this.head];
    delete this.items[this.head];
    this.head++;
    return item;
  }

  front() {
    return this.items[this.head];
  }
}

dequeue runs in four steps:

  1. First check whether the queue is empty, which is when head equals tail. If it is empty, return undefined.
  2. Copy the item in the box numbered head (the front item) into the variable item.
  3. Remove that box from the object with delete.
  4. Move head forward one, then return item.

Now enqueue, dequeue and front are all O(1): the number of steps stays the same however long the queue is, because only one box and one marker change. Queues are used for print queues, messages handled in the order they arrive, and tasks done one at a time.

⚠️ A queue that skips people, or counts below zero

TL;DR: A queue is a FIFO data structure (First In, First Out): the first item in is the first one out, like a checkout line. Enqueue puts an item at the back, dequeue takes an item from the front, and front looks at the front item without taking it. With an array, dequeue uses shift(), which moves every other item, so it's O(n). A faster way: keep the items in an object with numbers as keys, plus two markers (variables that hold a number), head (the number of the front item) and tail (the number for the next item). Enqueue puts the item at number tail, then adds one to tail. Dequeue takes the item at number head, deletes it, then adds one to head. The number of items is tail - head, and every operation becomes O(1).

⚡ First in, first out

Write:

const queue = ["A", "B", "C"]; // front to back

queue.push("D");            // enqueue: ["A", "B", "C", "D"]
console.log(queue[0]);      // A (front: only looked at)
console.log(queue.shift()); // A (dequeue)

Meaning: A queue is a FIFO data structure, First In, First Out: the first item in is the first one out, like a checkout line. In this array, index 0 is the front of the queue. Enqueue (push) puts an item at the back, dequeue (shift()) takes an item from the front, and front (queue[0]) only looks at the front item.

⚡ shift() moves everyone

Guess first: a queue of 1,000 people is stored in an array, and the person at the front is served with shift(). How many people move?

All 999 of the others. An array can't have gaps, so every other item moves forward one index. That's why shift() is O(n).

Meaning: Don't move the people, move the markers. A marker is a variable that holds the number of the front item. When the person at the front is served, only that number goes up by one.

⚡ The head and tail markers

Write:

enqueue(item) {
  this.items[this.tail] = item;
  this.tail++;
}

dequeue() {
  if (this.head === this.tail) return undefined; // empty
  const item = this.items[this.head];
  delete this.items[this.head];
  this.head++;
  return item;
}

Meaning: These two functions are methods of a Queue class (this means the queue in use). The items are kept in the object this.items with numbers as keys. head is the number of the front item, and tail is the number where the next item goes. enqueue puts the item at number tail, then moves tail forward (adds one to it). dequeue reads the item at number head first, removes it from the object with delete, and only then moves head forward. The number of items is tail - head, and every operation becomes O(1).

What you will learn