A deque relaxes the rules of both the stack and the queue: items can be added and removed at both ends. It can therefore behave like a stack, like a queue, or like a mix of the two.
Definition and terminology
Intuition
A deck of cards on a table: you can put a card on top or slide one under the bottom, and you can take a card from the top or from the bottom.
Definition 5.11 · Deque
A deque (double-ended queue, pronounced “deck”) is an ordered list in which items can be inserted and deleted at both ends, called the front and the rear.
Correction · 'Dequeue' versus 'deque'
Study material says: §5.6 calls the double-ended queue a “Dequeue”.
Correct version: The standard name is deque (double-ended queue). “Dequeue” is the usual name of the operation that removes an item from the front of a queue (the opposite of “enqueue”).
Why: Knuth (The Art of Computer Programming , Vol. 1, §2.2.1) and Cormen et al. (Introduction to Algorithms , §10.1) use “deque” for the structure and “dequeue” for the deletion operation. In the exam, write “deque (double-ended queue)” so the meaning is clear.
Two restricted versions are common:
Kind Insert at Delete at Deque Both ends Both ends Input-restricted deque Rear only Both ends Output-restricted deque Both ends Front only Stack (as a deque) One end Same end Queue (as a deque) Rear Front
Array implementation (circular)
A deque in an array works best as a circular array, so both ends can move in both directions. Keep front (index of the first item) and count:
add_front: front = (front - 1 + n) % n, store at front, count++;
add_rear: store at (front + count) % n, count++;
delete_front: read items[front], front = (front + 1) % n, count--;
delete_rear: read items[(front + count - 1) % n], count--.
Adding n before % n in add_front keeps the index from becoming negative when front is 0.
Example 5.12 A circular array deque of 6 cells
Goal. Add 20 and 30 at the rear, 10 and 5 at the front (wrapping below index 0), delete one item from each end, then fill the deque.
Program.
C · deque_array.c Copy #include <stdio.h>
#define MAX 6
typedef struct {
int items[MAX];
int front; /* index of the first element */
int count; /* number of elements */
} Deque; /* circular array: indices wrap around */
void create(Deque * d) { d->front = 0 ; d->count = 0 ; }
int add_front(Deque * d, int x)
{
if (d->count == MAX)
return 0 ;
d->front = (d->front - 1 + MAX) % MAX; /* step back, wrapping */
d->items[d->front] = x;
d->count ++ ;
return 1 ;
}
int add_rear(Deque * d, int x)
{
if (d->count == MAX)
return 0 ;
d->items[(d->front + d->count) % MAX] = x; /* cell after the last one */
d->count ++ ;
return 1 ;
}
int delete_front(Deque * d, int * x)
{
if (d->count == 0 )
return 0 ;
* x = d->items[d->front];
d->front = (d->front + 1 ) % MAX;
d->count -- ;
return 1 ;
}
int delete_rear(Deque * d, int * x)
{
if (d->count == 0 )
return 0 ;
* x = d->items[(d->front + d->count - 1 ) % MAX]; /* the last element */
d->count -- ;
return 1 ;
}
void show( const Deque * d)
{
printf( "front=%d count=%d deque:" , d->front, d->count);
for ( int k = 0 ; k < d->count; k ++ )
printf( " %d" , d->items[(d->front + k) % MAX]);
printf( " \n " );
}
int main( void )
{
Deque d;
int x;
create( & d);
add_rear( & d, 20 );
add_rear( & d, 30 );
add_front( & d, 10 ); /* front moves from 0 back to 5 */
add_front( & d, 5 );
show( & d);
delete_rear( & d, & x);
printf( "delete_rear -> %d \n " , x);
delete_front( & d, & x);
printf( "delete_front -> %d \n " , x);
show( & d);
for ( int v = 1 ; v <= 5 ; v ++ )
if ( ! add_rear( & d, v))
printf( "add_rear(%d): deque full \n " , v);
show( & d);
return 0 ;
}
Run it.
Output Copy front=4 count=4 deque: 5 10 20 30
delete_rear -> 30
delete_front -> 5
front=5 count=2 deque: 10 20
add_rear(5): deque full
front=5 count=6 deque: 10 20 1 2 3 4
Result. Adding at the front moved front from 0 back to 5 and then 4 without leaving the array; deletions at both ends worked, and the sixth item filled the deque.
Linked implementation (doubly linked list)
With a doubly linked list and head/tail pointers, all four operations are O ( 1 ) O(1) O ( 1 ) : each end can reach its neighbour through left or right. A singly linked list would make delete_rear O ( n ) O(n) O ( n ) , because the node before the tail has to be found.
Example 5.13 A deque as a doubly linked list
Goal. Add 2 and 1 at the front, 3 and 4 at the rear, delete from both ends, then empty the deque from the rear.
Program. Each delete updates the new end’s outer link to NULL, and both pointers become NULL when the deque is empty.
C · deque_linked.c Copy #include <stdio.h>
#include <stdlib.h>
typedef struct dnode {
struct dnode * left;
int info;
struct dnode * right;
} DNode;
typedef struct {
DNode * head; /* front end */
DNode * tail; /* rear end */
} Deque;
static DNode * make( int x)
{
DNode * n = malloc( sizeof * n);
if (n != NULL ) {
n->info = x;
n->left = n->right = NULL ;
}
return n;
}
int add_front(Deque * d, int x)
{
DNode * n = make(x);
if (n == NULL ) return 0 ;
n->right = d->head;
if (d->head) d->head->left = n; else d->tail = n;
d->head = n;
return 1 ;
}
int add_rear(Deque * d, int x)
{
DNode * n = make(x);
if (n == NULL ) return 0 ;
n->left = d->tail;
if (d->tail) d->tail->right = n; else d->head = n;
d->tail = n;
return 1 ;
}
int delete_front(Deque * d, int * x)
{
if (d->head == NULL ) return 0 ;
DNode * t = d->head;
* x = t->info;
d->head = t->right;
if (d->head) d->head->left = NULL ; else d->tail = NULL ;
free(t);
return 1 ;
}
int delete_rear(Deque * d, int * x)
{
if (d->tail == NULL ) return 0 ;
DNode * t = d->tail;
* x = t->info;
d->tail = t->left;
if (d->tail) d->tail->right = NULL ; else d->head = NULL ;
free(t);
return 1 ;
}
void show( const Deque * d)
{
printf( "head ->" );
for ( const DNode * p = d->head; p != NULL ; p = p->right)
printf( " %d" , p->info);
printf( " <- tail \n " );
}
int main( void )
{
Deque d = { NULL , NULL };
int x;
add_front( & d, 2 );
add_front( & d, 1 );
add_rear( & d, 3 );
add_rear( & d, 4 );
show( & d);
delete_front( & d, & x);
printf( "delete_front -> %d \n " , x);
delete_rear( & d, & x);
printf( "delete_rear -> %d \n " , x);
show( & d);
while (delete_rear( & d, & x))
printf( "delete_rear -> %d \n " , x);
printf( "head=%s tail=%s \n " , d.head ? "set" : "NULL" , d.tail ? "set" : "NULL" );
return 0 ;
}
Run it.
Output Copy head -> 1 2 3 4 <- tail
delete_front -> 1
delete_rear -> 4
head -> 2 3 <- tail
delete_rear -> 3
delete_rear -> 2
head=NULL tail=NULL
Result. All four operations work in O ( 1 ) O(1) O ( 1 ) , and the deque ends with head and tail both NULL.
Correction · Programs 5.8 and 5.9 (deque)
Study material says: Program 5.8 (§5.6.1) writes #define QUEUE_LENGTH 10; (with a semicolon), uses = = with a space, starts with front = rear = -1, and add_front computes front = front - 1 and stores dq[front] = y; add_rear tests front == QUEUE_LENGTH - 1. Program 5.9 (§5.6.2) declares the links as int *left, *right, uses p_right, dcons and a type dq that do not exist, and mixes two different designs.
Correct version: Use a circular array with front and count (as above) so both ends can move freely and the index stays inside the array, or a doubly linked list with struct dnode *left, *right, head and tail (as above). Test the rear (or the count) for “full” when adding at the rear.
Why: The semicolon in the #define turns int dq[QUEUE_LENGTH]; into int dq[10;];, and = = is not an operator, so Program 5.8 does not compile; Program 5.9 uses undeclared names and incompatible pointer types. In Program 5.8’s logic, the first add_front on an empty deque (front = -1) computes front = -2 and writes dq[-2], outside the array (confirmed by a test run).
Key idea
A deque allows insertion and deletion at both ends; implemented as a circular array or a doubly linked list, all four operations take O ( 1 ) O(1) O ( 1 ) , and stacks and queues are special cases of it.
TEE Exam note
For “what is a deque? Implement it”: define it (and write “deque”, not “dequeue”), name the input- and output-restricted variants, and give either the circular-array version (with the modulo formulas for both ends) or the doubly-linked version (with head and tail). Check Your Progress 2, Q1: a deque allows elements to be added and deleted at the front as well as the rear.
Common mistake
Computing the new front as (front - 1) % n. In C, -1 % n is −1, so the index becomes negative; use (front - 1 + n) % n.
Common mistake
Implementing a deque with a singly linked list and claiming all operations are O ( 1 ) O(1) O ( 1 ) . Deleting at the rear needs the previous node, which a singly linked list cannot reach in O ( 1 ) O(1) O ( 1 ) .
Key takeaways
Deque = insert and delete at both ends; input-restricted and output-restricted deques limit one side.
Circular array: front moves with (front ± 1 + n) % n; the rear is (front + count - 1) % n.
Doubly linked list with head and tail: all four operations O ( 1 ) O(1) O ( 1 ) .
Stacks and queues are special cases of a deque.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice Which structure allows insertion and deletion at both ends?
A Stack B Queue C Priority queue D Deque
Check answer
Show answer Answer: D. Deque
That is the definition of a double-ended queue (Check Your Progress 2, Q1). A stack uses one end, a queue uses one end for each operation.
Question 2 · multiple choice In an input-restricted deque, where can items be inserted?
A At one end only B At both ends C Only in the middle D Nowhere after creation
Check answer
Show answer Answer: A. At one end only
“Input-restricted” limits insertion to one end while deletion is allowed at both. An output-restricted deque is the reverse.
Question 3 · multiple choice A circular array deque has n = 6, front = 0. Where does add_front store the new item?
A Index −1 B Index 1 C Index 5 D Index 0
Check answer
Show answer Answer: C. Index 5
(0 - 1 + 6) % 6 = 5; the front wraps to the last cell. Without the + 6, C would compute −1.
Question 4 · multiple choice What does this program print?
C · q5-deque.c Copy #include <stdio.h>
#define N 5
int main( void )
{
int d[N], front = 0 , count = 0 ;
d[(front + count ++ ) % N] = 1 ; /* add_rear 1 */
d[(front + count ++ ) % N] = 2 ; /* add_rear 2 */
front = (front - 1 + N) % N; /* add_front 9 */
d[front] = 9 ; count ++ ;
int r = d[(front + count - 1 ) % N]; /* delete_rear */
count -- ;
printf( "front=%d removed=%d first=%d count=%d \n " , front, r, d[front], count);
return 0 ;
} A front=4 removed=9 first=9 count=2 B front=4 removed=2 first=9 count=2 C front=0 removed=2 first=1 count=2 D front=4 removed=1 first=9 count=3
Check answer
Show answer Answer: B. front=4 removed=2 first=9 count=2
The deque reads 9 1 2 (front at index 4). Deleting at the rear removes 2 and leaves 9 1. Removing 9 would be delete_front.
Question 5 · multiple choice Why is a doubly linked list preferred to a singly linked list for a deque?
Check answer
Show answer Answer: C. Deleting at the rear needs the previous node, which only a doubly linked list gives in O ( 1 ) O(1) O ( 1 ) .
With only next links, finding the node before the tail is O ( n ) O(n) O ( n ) . Adding at the front is O ( 1 ) O(1) O ( 1 ) in both kinds of list.
Question 6 · short answer
Write the four deque operations for a circular array of size n that keeps front and count.
Show worked answer add_front(x): if count == n, overflow; else front = (front - 1 + n) % n; d[front] = x; count++. add_rear(x): if count == n, overflow; else d[(front + count) % n] = x; count++. delete_front(): if count == 0, underflow; else x = d[front]; front = (front + 1) % n; count--; return x. delete_rear(): if count == 0, underflow; else x = d[(front + count - 1) % n]; count--; return x. All four are O ( 1 ) O(1) O ( 1 ) .