Double-Ended Queue
Insert and delete at BOTH endsA deque (say “deck”) is a queue that works from both ends: you can insert at the front or the rear, and delete from the front or the rear. It is stored in a circular array, so both front and rear wrap around. Try the two restricted forms too: an input-restricted deque inserts at one end only, an output-restricted deque deletes at one end only.
- Frontinsert or delete here
- Rearinsert or delete here too
- Wrap-aroundfront − 1 and rear + 1, % N
- Restrictedinput- or output-restricted
- Current
- New / found
- Leaving
- Empty
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
A circular array with two moving ends
A deque is stored like a circular queue: an array of N slots and two indexes, front and rear (both −1 when it is empty). The difference is that BOTH ends move in BOTH directions. Inserting at the rear moves rear forward, inserting at the front moves front backward; deleting at the front moves front forward, deleting at the rear moves rear backward. Every step wraps around with % N.
#define N 8 int dq[N]; int front = -1, rear = -1; // -1 means empty // one step forward: i = (i + 1) % N // one step back: i = (i - 1 + N) % N // the + N keeps it from going negative
Insert Rear
- Check the deque is not full.
- If it is empty: front = rear = 0.
- Otherwise rear = (rear + 1) % N.
- Store the value at dq[rear].
Insert Front
- Check the deque is not full.
- If it is empty: front = rear = 0.
- Otherwise front = (front − 1 + N) % N: one step BACK.
- Store the value at dq[front].
Delete Front
- Check the deque is not empty.
- Take x = dq[front].
- If front == rear it was the last item: front = rear = −1.
- Otherwise front = (front + 1) % N.
Delete Rear
- Check the deque is not empty.
- Take x = dq[rear].
- If front == rear it was the last item: front = rear = −1.
- Otherwise rear = (rear − 1 + N) % N: one step BACK.
Peek, Is Empty, Is Full
- Peek Front returns dq[front]; Peek Rear returns dq[rear].
- Is Empty: front == −1.
- Is Full: (rear + 1) % N == front.
- None of them changes the deque.
Wrap-around
- Stepping back from index 0 lands on N − 1.
- Stepping on from index N − 1 lands on 0.
- The + N in (i − 1 + N) % N avoids a negative index.
- So no slot is ever wasted.
Input-restricted deque
- Insert at ONE end only: the rear.
- Delete at both ends.
- Insert Front is not allowed.
- Useful when you control what goes in but may remove from either side.
Output-restricted deque
- Delete at ONE end only: the front.
- Insert at both ends.
- Delete Rear is not allowed.
- Useful when items arrive from either side but must leave in one order.
Where deques are used
- Undo history: new actions at one end, the oldest dropped from the other.
- Sliding-window problems (the window's maximum).
- Work stealing between processors.
- A deque can act as a stack OR as a queue.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of items. Every deque operation changes at most two indexes and one slot, so each one takes O(1) time whatever the size. The array needs O(N) space for its N slots.
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Insert Front / Insert Rear | O(1) |
O(1) |
O(1) |
Instant |
| Delete Front / Delete Rear | O(1) |
O(1) |
O(1) |
Instant |
| Peek Front / Peek Rear | O(1) |
O(1) |
O(1) |
Instant |
| Is Empty / Is Full | O(1) |
O(1) |
O(1) |
Instant |
Stack, queue and deque
| Stack | Queue | Deque | |
|---|---|---|---|
| Insert at | the top | the rear | the front or the rear |
| Delete from | the top | the front | the front or the rear |
| Order | LIFO | FIFO | both |
| Can it replace another? | no | no | yes: a stack or a queue |
The three kinds of deque
| Kind | Insert at | Delete from | Not allowed |
|---|---|---|---|
| Deque | front and rear | front and rear | nothing |
| Input-restricted | rear only | front and rear | Insert Front |
| Output-restricted | front and rear | front only | Delete Rear |
Check your understanding
Answer each question — you will see the explanation straight away.
Which deque operation moves front BACKWARD?
A new item goes IN FRONT of the current front, so front steps back one slot: front = (front − 1 + N) % N.
N = 8 and front = 0. You insert at the front. Where does front go?
(0 − 1 + 8) % 8 = 7: it wraps around to the last slot.
An INPUT-restricted deque does NOT allow…
Input-restricted: items go in at ONE end only (the rear). It can still delete at both ends.
An OUTPUT-restricted deque does NOT allow…
Output-restricted: items come out at ONE end only (the front). It can still insert at both ends.
How long does Insert Front take in a circular-array deque?
Only front changes, and one slot is written: nothing is shifted.
How can you tell that the deque is full?
If rear stepping on one slot would land on front, every slot is in use.
front == rear and you Delete Front. What happens?
front == rear means there was just one item. After removing it, both indexes go back to −1.
Queues quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.