Deques

A queue you can use from both ends. Seven steps: pushing and popping at the front and the back, two ways to build one, and two jobs it does well.

Beginner7 steps~6 min
Item — one value stored in the deque
Front — the first item; one open end
Back — the last item; the other open end
New item — just pushed in this step
// THE SHORT VERSION

In 30 seconds

A deque (double-ended queue) is a line of items you can add to or remove from at either end, each in one quick step, however long the line is.

It is usually built as an array whose slot numbers wrap round, or as a chain of nodes linked in both directions.

Use only one end and you have a stack; put in at one end and take out at the other and you have a queue. Having both ends is what makes sliding-window tricks and work-stealing schedulers fast.

Deques, step by step

STEPS01 / 07

In a ring array a push is O(1) on average (amortised): now and then the array is full and must be copied into a larger one. Real work-stealing deques also need careful locking or atomic operations.

STEP 01 / 07 A line open at both ends
THE MATH
deque = [3, 7, 5] front = 3 back = 5
  1. Step 1: A line open at both ends. A deque (said “deck”, short for double-ended queue) keeps items in order, like a queue. The difference is that both ends are open: you may add or remove at the front and at the back, but not in the middle.
  2. Step 2: Push at either end. push_front(1) puts 1 before the old front; push_back(9) puts 9 after the old back. The items already inside keep their places, so each push is one step however long the deque is: O(1).
  3. Step 3: Pop at either end. pop_front removes and returns the first item, pop_back the last, again in O(1). Push and pop at the same end and the deque behaves as a stack; push at the back and pop at the front and it is an ordinary queue.
  4. Step 4: Built as a ring array. One way to build it: a fixed array plus a head index, with slot numbers that wrap round. push_front steps head back one slot, from 0 round to 7, and writes there; push_back writes after the last item. Nothing is shifted. When the array fills up, it is copied into a bigger one.
  5. Step 5: Built as a linked list. The other classic build: a doubly linked list. Every node points to its two neighbours and the deque keeps a pointer to each end, so adding or removing there is O(1) and it never needs copying. The price is extra memory per item and no quick jump to the middle. Many libraries mix the two: a chain of small arrays.
  6. Step 6: Use: sliding window maximum. To report the largest of the last 3 values as a window slides along, keep a deque of candidates, biggest at the front. When 5 arrives, 3 is dropped from the back (smaller and older, it can never win) and 9 from the front (it left the window). The front is the answer: 6. Each value enters and leaves once.
  7. Step 7: Use: work stealing. Schedulers that spread tasks over processor cores give every worker its own deque. A worker adds and takes tasks at the back of its deque. A worker with nothing to do steals from the front of someone else’s, taking the oldest task. Owner and thief work at opposite ends, so they seldom get in each other’s way.
VALUES SO FAR
deque[3, 7, 5]start
pushed····result
cost····rule
popped····result
capacity····array
head····index
window····input
maxima····result
thief····rule

Spotted a mistake on this page?