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.
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
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 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.
- 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).
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- Circular buffersA fixed array reused as an endless loop, with a head and a tail that wrap round. Beginner
- Sliding window techniqueA frame moves along a list, updating its total instead of adding everything again. Beginner
- Priority queuesA queue where the most urgent item leaves first, and the heap that makes it fast. Beginner
Spotted a mistake on this page?