Essential Data Structures home

Deque

A double-ended queue. You can add or remove items at either end, so one structure can act as a queue, a stack, or both at once.

A B C left end right end add or remove add or remove Both ends are open. Add or remove at the left, or add or remove at the right.
Illustration: a deque of three items. Either end can take new items or give items back.

What it is

A deque, pronounced "deck", is a queue with both ends open. You can add an item at the left end or the right end, and you can remove an item from the left end or the right end. Peek looks at an end item without removing it. The middle stays off limits, just as it does for a queue or a stack.

That freedom makes a deque the general case. Use only the right end and it behaves like a stack. Use the right end to add and the left end to remove and it behaves like a queue. Use both ends and it is something neither of those can do on its own, such as a sliding window that drops old items from one end while new ones arrive at the other.

A deque is the mirror image of a queue and a stack at once. A queue opens one end for adding and the other for removing, a stack opens a single end for both, and a deque opens both ends for both.

How it works

Adding or removing at either end each takes constant time, as long as the deque is built the right way. The classic implementation is a circular buffer: a fixed array where both ends wrap around when they reach the edge, so freed space is reused. A doubly linked list works too, with a pointer to each end. Python's collections.deque is a doubly linked list of small blocks, which keeps both ends fast while avoiding one node per item.

What does not work well is a plain list used with insert(0, x) or pop(0), because adding or removing at the front shifts every item after it, which costs linear time. A deque exists to avoid exactly that. The two edges to watch are the same as for a queue: adding to a full fixed-size deque is an overflow, and removing from an empty deque is an underflow. A dynamic deque grows instead of overflowing, and a careful program checks for empty before it removes.

Complexity

OperationAverageWorst case
Add at right (append)O(1)O(1) amortized on a dynamic array
Add at left (appendleft)O(1)O(1)
Remove from right (pop)O(1)O(1)
Remove from left (popleft)O(1)O(1)
Peek at either endO(1)O(1)
Search by valueO(n)O(n)

A circular buffer or a doubly linked list gives O(1) adds and removes at both ends in the worst case. Indexing into the middle is not a deque's strength: Python's deque indexes in O(n) in the worst case, while a circular buffer can index in O(1). Searching by value is O(n) either way.

When to use it, and when not to

Use a deque when work arrives at both ends, or when you need a queue but sometimes need to push an item back to the front: a sliding window over recent data, an undo history that can also redo, a work-stealing scheduler where workers take from one end and steal from the other, and a palindrome check that compares the two ends inward. If you need both a stack and a queue in one place, a deque is the single structure.

Do not use a deque when you need to reach an item in the middle, find something by value, or access items by index. Reaching into the middle is what an array is for, and finding by value is what a hash table is for.

Where you meet it

Anywhere a program keeps a moving window of recent items. A browser history lets you go back and forward from either end. A text editor's undo stack can also redo from the other side. A task scheduler holds jobs in a deque so a worker can take from the front while a high-priority job jumps in at the front too. Sliding-window algorithms, such as finding the maximum of each window of k numbers, keep their candidates in a deque.

In Python

from collections import deque

d = deque()

d.append("B")       # add at the right
d.appendleft("A")   # add at the left
d.append("C")       # add at the right

print(d[0])         # peek at the left: A
print(d[-1])        # peek at the right: C
print(d.popleft())  # remove from the left: A
print(d.pop())      # remove from the right: C
print(list(d))      # only B remains

In short: a deque is a queue with both ends open. Four operations, all constant time, and one structure that can play the part of a queue, a stack, or both.

Sources

← Home