Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 1 · syllabusAsked 4× in KTU papersChecked by code · teacher review pending

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
ormakkurippu ✎
Never-forget card

Deque = Double-Ended QUEue: insert AND delete at BOTH ends. Input-restricted: insert at one end only. Output-restricted: delete at one end only.

Picture it

A local bus with front and back doors: people can get on or off at either door.

Write this in the exam

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

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.

VariantInsert atDelete at
Dequefront and rearfront and rear
Input-restricted dequerear onlyfront and rear
Output-restricted dequefront and rearfront 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.

  1. InsertFront(x): if full → overflow. If empty, front = rear = 0; else front ← (front − 1 + MAX) mod MAX. dq[front] ← x.
  2. InsertRear(x): if full → overflow. If empty, front = rear = 0; else rear ← (rear + 1) mod MAX. dq[rear] ← x.
  3. DeleteFront(): if empty → underflow. item ← dq[front]. If front = rear, front = rear = −1; else front ← (front + 1) mod MAX.
  4. DeleteRear(): if empty → underflow. item ← dq[rear]. If front = rear, front = rear = −1; else rear ← (rear − 1 + MAX) mod MAX.
Pro tip

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

Worked example: MAX = 5 — insertFront wraps front from 0 to 4checked by code
OperationResultfront, rearArray (index 0 … 4)Front → rear
insertRear(10)ok0, 0[10 _ _ _ _]10
insertRear(20)ok0, 1[10 20 _ _ _]10 20
insertFront(5)ok4, 1[10 20 _ _ 5]5 10 20
insertFront(1)ok3, 1[10 20 _ 1 5]1 5 10 20
insertRear(30)ok3, 2[10 20 30 1 5]1 5 10 20 30
insertFront(0)OVERFLOW3, 2[10 20 30 1 5]1 5 10 20 30
deleteFrontremoved 14, 2[10 20 30 _ 5]5 10 20 30
deleteRearremoved 304, 1[10 20 _ _ 5]5 10 20
deleteRearremoved 204, 0[10 _ _ _ 5]5 10
deleteFrontremoved 50, 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 ◀ ▶.

Loading visualizer…

Algorithm / pseudocode

Double-ended queue (circular array)
  1. 1front ← −1; rear ← −1
  2. 2InsertFront(x):
  3. 3if (rear + 1) mod MAX = front then overflow
  4. 4print "Deque overflow"; return
  5. 5if front = −1 then front ← 0; rear ← 0
  6. 6else front ← (front − 1 + MAX) mod MAX
  7. 7dq[front] ← x
  8. 8InsertRear(x):
  9. 9if (rear + 1) mod MAX = front then overflow
  10. 10print "Deque overflow"; return
  11. 11if front = −1 then front ← 0; rear ← 0
  12. 12else rear ← (rear + 1) mod MAX
  13. 13dq[rear] ← x
  14. 14DeleteFront():
  15. 15if front = −1 then underflow
  16. 16print "Deque underflow"; return
  17. 17item ← dq[front]
  18. 18if front = rear then front ← −1; rear ← −1
  19. 19else front ← (front + 1) mod MAX
  20. 20DeleteRear():
  21. 21if front = −1 then underflow
  22. 22print "Deque underflow"; return
  23. 23item ← dq[rear]
  24. 24if front = rear then front ← −1; rear ← −1
  25. 25else rear ← (rear − 1 + MAX) mod MAX
  26. 26return item
  27. 27end

C implementation

deque_array.ctested ✓ 200/200 cases
C
/* 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

Open in the Code Lab →

Complexity

All four operations are O(1) with a circular array (or with a doubly linked list).

Deque vs priority queue (model paper Q10b)

DequePriority queue
Order of removalby position (front or rear)by priority (highest/lowest first)
Typical structurecircular array / doubly linked listbinary heap (Module 3)
Insert / delete costO(1) / O(1)O(log n) / O(log n) with a heap
Use casessliding windows, work stealing, undoscheduling 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)

Quick check — can you get them all?

Question 1

Q1.insertFront in a circular-array deque moves front to…

Question 2

Q2.An output-restricted deque allows…

Question 3

Q3.A deque can act as…

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)