Deque (Double-Ended Queue) in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Insert and Remove at Both Ends

A deque (double-ended queue) allows insertion and removal at both ends: push_front, push_back, pop_front, pop_back. It generalizes both stacks and queues. A circular array handles it neatly: pushing at the front moves head backward with modulo.

Remember
push_back : data[(head + count) % CAP] = v;
push_front: head = (head + CAP - 1) % CAP; data[head] = v;
pop_front : v = data[head]; head = (head + 1) % CAP;
pop_back  : v = data[(head + count - 1) % CAP];

Cheat Sheet

Keep these facts handy while you work with Deque (Double-Ended Queue) in C in C.

TopicDetail
Stack modeUse one end only
Queue modepush_back + pop_front
All opsO(1)
Move head back+ CAP before %

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Priority Queue.

Example 1 — A deque of four slots

Push at both ends and pop at both ends.

C
#include <stdio.h>
#define CAP 4

struct Deque { int data[CAP]; int head, count; };

static void push_back(struct Deque *d, int v) {
    d->data[(d->head + d->count) % CAP] = v;
    d->count++;
}
static void push_front(struct Deque *d, int v) {
    d->head = (d->head + CAP - 1) % CAP;
    d->data[d->head] = v;
    d->count++;
}
static int pop_front(struct Deque *d) {
    int v = d->data[d->head];
    d->head = (d->head + 1) % CAP;
    d->count--;
    return v;
}
static int pop_back(struct Deque *d) {
    int v = d->data[(d->head + d->count - 1) % CAP];
    d->count--;
    return v;
}
static void show(const struct Deque *d) {
    printf("deque:");
    for (int i = 0; i < d->count; i++) printf(" %d", d->data[(d->head + i) % CAP]);
    printf("\n");
}

int main(void) {
    struct Deque d = {{0}, 0, 0};
    push_back(&d, 2); push_back(&d, 3);
    push_front(&d, 1); push_front(&d, 0);
    show(&d);
    printf("pop_back: %d\n", pop_back(&d));
    printf("pop_front: %d\n", pop_front(&d));
    show(&d);
    return 0;
}

How It Works

  1. push_front wraps head backward from 0 to slot 3, then to slot 2.
  2. Reading from head with modulo gives 0 1 2 3.
  3. Pops remove 3 from the back and 0 from the front.

Edge Cases & Pitfalls

Check these before relying on Deque (Double-Ended Queue) in C in real programs.

neg

Negative index

(head - 1) % CAP can be negative in C. Add CAP first.

cap

No full check

Add count == CAP checks to every push in real code.

Key Takeaways

  • Both ends O(1).
  • Generalizes stack and queue.
  • Add CAP before modulo when stepping back.

One line: A deque (double-ended queue) allows insertion and removal at both ends: push_front, push_back, pop_front, pop_back.

Frequently Asked Questions

A double-ended queue that supports insertion and deletion at both the front and the back.
Yes. Use push_back and pop_back (or the front pair) and it behaves as LIFO.
Continue with "C Priority Queue" next. Dequeue by priority instead of arrival order. The C Queues hub lists every topic in order.

Next: Priority Queue

Dequeue by priority instead of arrival order.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful