MCS-208 · Unit 5

Queues

Section 8 of 8

Block 2 · Unit 5Queues15 min · Section 8 of 8

5.8Deques (Double-Ended Queues)

Covers IGNOU MCS-208 §5.6, 5.6.1, 5.6.2

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

Two restricted versions are common:

KindInsert atDelete at
DequeBoth endsBoth ends
Input-restricted dequeRear onlyBoth ends
Output-restricted dequeBoth endsFront only
Stack (as a deque)One endSame end
Queue (as a deque)RearFront

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.

Linked implementation (doubly linked list)

With a doubly linked list and head/tail pointers, all four operations are 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), because the node before the tail has to be found.

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).
  • 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?

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?

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?

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
#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;
}
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?

Show answer

Answer: C. Deleting at the rear needs the previous node, which only a doubly linked list gives in O(1)O(1).

With only next links, finding the node before the tail is O(n)O(n). Adding at the front is 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).

↑ ↓ move↵ openTab switch type