Double-ended queues
A double-ended queue: insert and delete at both the front and the rear.
Syllabus says: “Double Ended Queues”
On this page · 19 sections
Deque = Double-Ended QUEue: insert AND delete at BOTH ends. Input-restricted: insert at one end only. Output-restricted: delete at one end only.
A local bus with front and back doors: people can get on or off at either door.
A double-ended queue (deque) is a linear list in which insertions and deletions can be made at both the front and the rear.
Learning objectives
- Define a deque and its restricted variants.
- Write the insertion operations (insertFront, insertRear — KTU Nov 2025 Q9b).
- Write the deletion operations (deleteFront, deleteRear — KTU May 2026 Q9b, model paper Q11b).
Prerequisites
Circular queues; modulo arithmetic with a negative step: (i − 1 + MAX) mod MAX.
Introduction
A deque (pronounced ‘deck’) combines a stack and a queue. A browser history that also discards its oldest page when full, or a ‘steal from the other end’ task scheduler, both need insertion and deletion at both ends.
Definition
A double-ended queue (deque) is a linear list in which elements can be inserted or deleted at either end, the front or the rear.
| Variant | Insert at | Delete at |
|---|---|---|
| Deque | front and rear | front and rear |
| Input-restricted deque | rear only | front and rear |
| Output-restricted deque | front and rear | front only |
A deque used only through insertRear and deleteFront behaves as a queue. Used only through insertRear and deleteRear, it behaves as a stack.
Circular-array implementation
Use a circular array with front = rear = −1 for empty. Full is (rear + 1) mod MAX = front, exactly as in a circular queue.
- InsertFront(x): if full → overflow. If empty, front = rear = 0; else front ← (front − 1 + MAX) mod MAX. dq[front] ← x.
- InsertRear(x): if full → overflow. If empty, front = rear = 0; else rear ← (rear + 1) mod MAX. dq[rear] ← x.
- DeleteFront(): if empty → underflow. item ← dq[front]. If front = rear, front = rear = −1; else front ← (front + 1) mod MAX.
- DeleteRear(): if empty → underflow. item ← dq[rear]. If front = rear, front = rear = −1; else rear ← (rear − 1 + MAX) mod MAX.
Why + MAX? In C, (0 − 1) % 5 is −1, not 4. Adding MAX first, (0 − 1 + 5) % 5 = 4, keeps the index valid.
Where it is used
- Work-stealing schedulers (a thread takes work from the other end of another thread's deque).
- Sliding-window maximum/minimum algorithms.
- Undo history with a size limit (drop the oldest entry from the other end).
Worked example
| Operation | Result | front, rear | Array (index 0 … 4) | Front → rear |
|---|---|---|---|---|
| insertRear(10) | ok | 0, 0 | [10 _ _ _ _] | 10 |
| insertRear(20) | ok | 0, 1 | [10 20 _ _ _] | 10 20 |
| insertFront(5) | ok | 4, 1 | [10 20 _ _ 5] | 5 10 20 |
| insertFront(1) | ok | 3, 1 | [10 20 _ 1 5] | 1 5 10 20 |
| insertRear(30) | ok | 3, 2 | [10 20 30 1 5] | 1 5 10 20 30 |
| insertFront(0) | OVERFLOW | 3, 2 | [10 20 30 1 5] | 1 5 10 20 30 |
| deleteFront | removed 1 | 4, 2 | [10 20 30 _ 5] | 5 10 20 30 |
| deleteRear | removed 30 | 4, 1 | [10 20 _ _ 5] | 5 10 20 |
| deleteRear | removed 20 | 4, 0 | [10 _ _ _ 5] | 5 10 |
| deleteFront | removed 5 | 0, 0 | [10 _ _ _ _] | 10 |
MAX = 5. “_” marks a slot that holds no live element.
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithm / pseudocode
- 1front ← −1; rear ← −1
- 2InsertFront(x):
- 3if (rear + 1) mod MAX = front then overflow
- 4print "Deque overflow"; return
- 5if front = −1 then front ← 0; rear ← 0
- 6else front ← (front − 1 + MAX) mod MAX
- 7dq[front] ← x
- 8InsertRear(x):
- 9if (rear + 1) mod MAX = front then overflow
- 10print "Deque overflow"; return
- 11if front = −1 then front ← 0; rear ← 0
- 12else rear ← (rear + 1) mod MAX
- 13dq[rear] ← x
- 14DeleteFront():
- 15if front = −1 then underflow
- 16print "Deque underflow"; return
- 17item ← dq[front]
- 18if front = rear then front ← −1; rear ← −1
- 19else front ← (front + 1) mod MAX
- 20DeleteRear():
- 21if front = −1 then underflow
- 22print "Deque underflow"; return
- 23item ← dq[rear]
- 24if front = rear then front ← −1; rear ← −1
- 25else rear ← (rear − 1 + MAX) mod MAX
- 26return item
- 27end
C implementation
deque_array.ctested ✓ 200/200 cases/* Double-ended queue using a circular array — KTU PCCST303, Module 1
* front = rear = -1 means empty. Full when (rear + 1) % MAX == front.
* Input : MAX (1..100), then commands until EOF:
* INSERTFRONT x | INSERTREAR x | DELETEFRONT | DELETEREAR */
#include <stdio.h>
#include <string.h>
#define CAP 100
int dq[CAP];
int front = -1, rear = -1;
int MAX;
int is_full(void) { return (rear + 1) % MAX == front; }
int is_empty(void) { return front == -1; }
void insert_front(int x) {
if (is_full()) { printf("OVERFLOW\n"); return; }
if (is_empty()) { front = 0; rear = 0; }
else front = (front - 1 + MAX) % MAX; /* step back, wrapping */
dq[front] = x;
printf("INSERTED %d\n", x);
}
void insert_rear(int x) {
if (is_full()) { printf("OVERFLOW\n"); return; }
if (is_empty()) { front = 0; rear = 0; }
else rear = (rear + 1) % MAX;
dq[rear] = x;
printf("INSERTED %d\n", x);
}
void delete_front(void) {
if (is_empty()) { printf("UNDERFLOW\n"); return; }
int item = dq[front];
if (front == rear) { front = -1; rear = -1; }
else front = (front + 1) % MAX;
printf("DELETED %d\n", item);
}
void delete_rear(void) {
if (is_empty()) { printf("UNDERFLOW\n"); return; }
int item = dq[rear];
if (front == rear) { front = -1; rear = -1; }
else rear = (rear - 1 + MAX) % MAX;
printf("DELETED %d\n", item);
}
int main(void) {
char cmd[16];
int x;
if (scanf("%d", &MAX) != 1 || MAX < 1 || MAX > CAP) return 1;
while (scanf("%15s", cmd) == 1) {
if (strcmp(cmd, "INSERTFRONT") == 0) { if (scanf("%d", &x) != 1) return 1; insert_front(x); }
else if (strcmp(cmd, "INSERTREAR") == 0) { if (scanf("%d", &x) != 1) return 1; insert_rear(x); }
else if (strcmp(cmd, "DELETEFRONT") == 0) delete_front();
else if (strcmp(cmd, "DELETEREAR") == 0) delete_rear();
else return 1;
}
printf("STATE front=%d rear=%d\nCONTENTS:", front, rear);
if (front != -1)
for (int i = front; ; i = (i + 1) % MAX) {
printf(" %d", dq[i]);
if (i == rear) break;
}
printf("\n");
return 0;
}
Sample run · MAX = 4 · real output from gcc
Input (stdin)
4 INSERTREAR 10 INSERTFRONT 5 INSERTREAR 20 DELETEFRONT DELETEREAR
Output (stdout)
INSERTED 10 INSERTED 5 INSERTED 20 DELETED 5 DELETED 20 STATE front=0 rear=0 CONTENTS: 10
Complexity
All four operations are O(1) with a circular array (or with a doubly linked list).
Deque vs priority queue (model paper Q10b)
| Deque | Priority queue | |
|---|---|---|
| Order of removal | by position (front or rear) | by priority (highest/lowest first) |
| Typical structure | circular array / doubly linked list | binary heap (Module 3) |
| Insert / delete cost | O(1) / O(1) | O(log n) / O(log n) with a heap |
| Use cases | sliding windows, work stealing, undo | scheduling by priority, Dijkstra, event simulation |
Edge cases
- insertFront on an empty deque puts the element at index 0 (front = rear = 0).
- Deleting the only element (front = rear) resets both to −1, from either end.
Common mistakes
- front − 1 without wrap-around → index −1.
- Updating the wrong pointer: insertFront changes front, deleteRear changes rear.
- Using rear = MAX − 1 as the full test.
Asked in KTU exams (4)
- November 2025 · Q9b3 markssolved ✓Write algorithm for various insertion operations of a double ended queue using array.
- May 2026 · Q9b4 markssolved ✓Write algorithm for various deletion operations of double ended queue using array.
- — 2024 · Q10b4 marksCompare priority queues and deques in terms of structure, use cases, and operations.
- — 2024 · Q11b4 markssolved ✓Write algorithms to insert and delete elements from a double ended queue.
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Four operations: insertFront, insertRear, deleteFront, deleteRear.
- Front moves back with (front − 1 + MAX) mod MAX; rear moves back with (rear − 1 + MAX) mod MAX.
- Input-restricted: insert at one end only. Output-restricted: delete at one end only.
- All O(1).
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — Ch. 3 (Stacks and Queues) (exercises on deques)
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §10.1 (Stacks and queues) (Exercise 10.1-5)
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)