Definition

Concept

Deque (double-ended queue) is an abstract data type supporting insertion and deletion at both ends. Combines properties of stacks and queues. Allows flexible data management with efficient access at front and rear.

Structure

Consists of linear collection where elements can be added or removed from front (head) and rear (tail). Supports both FIFO and LIFO operations depending on usage.

Terminology

Operations include: push_front, push_back, pop_front, pop_back. Also called dequeue or head-tail queue in some literature.

History

Origin

Introduced in computer science literature in the 1960s along with stacks and queues to generalize data structures for flexibility.

Evolution

Evolved with advances in algorithms and memory management. Incorporated in many libraries and programming languages as standard containers.

Significance

Key component in scheduling, buffering, and algorithm design. Facilitates complex data manipulations not feasible with simple stacks or queues.

Types of Deques

Input-Restricted Deque

Insertion allowed at only one end; deletion allowed at both ends. Restricts input operations to reduce complexity.

Output-Restricted Deque

Deletion allowed at only one end; insertion allowed at both ends. Prioritizes controlled output flow.

General Deque

Supports insertion and deletion at both ends without restriction. Most flexible and commonly used variant.

Implementation Techniques

Array-Based

Uses fixed-size or dynamically resizable arrays. Circular buffer technique employed to use space efficiently and achieve O(1) time for end operations.

Doubly Linked List

Each element points to previous and next nodes. Enables efficient insertion and deletion anywhere, especially at ends, with dynamic sizing.

Block Linked List

Combines arrays and linked lists: array blocks linked together to balance memory locality and dynamic expansion.

Memory Management

Considerations include dynamic resizing, allocation overhead, and cache locality depending on implementation.

Core Operations

push_front

Inserts element at front. Time: O(1) typical. Adjusts front pointer or head reference.

push_back

Inserts element at rear. Time: O(1) typical. Adjusts rear pointer or tail reference.

pop_front

Removes element from front. Returns element. Time: O(1). Manages underflow conditions.

pop_back

Removes element from rear. Returns element. Time: O(1). Similar underflow checks.

peek_front and peek_back

Accesses front or rear element without removal. Time: O(1).

Time and Space Complexity

Time Complexity

All core operations (insertion, deletion, access at ends): O(1) amortized for array, O(1) worst-case for linked list.

Space Complexity

O(n) where n is number of elements. Overhead depends on implementation: arrays require contiguous memory, linked lists require pointers.

Amortized Costs

Resizing arrays incur O(n) cost spread over multiple operations. Linked lists avoid resizing overhead but use extra pointer memory.

OperationArray-BasedLinked List
push_frontO(1) amortizedO(1)
push_backO(1) amortizedO(1)
pop_frontO(1)O(1)
pop_backO(1)O(1)
SpaceO(n)O(n + pointers)

Applications

Scheduling Algorithms

Used in CPU scheduling queues, task management with priority adjustments at both ends.

Sliding Window Problems

Efficiently maintains maximum, minimum, or sum over windows in arrays or streams. Common in real-time analytics.

Palindrome Checking

Enables comparison of front and rear elements for string symmetry detection.

Undo-Redo Mechanisms

Stores operations allowing bidirectional traversal for state restoration.

Memory Management

Buffers and caches with flexible insertion and deletion at variable ends.

Comparison with Stacks and Queues

Stack

LIFO structure. Single-end insertion and deletion. Deque generalizes stack by allowing operations at both ends.

Queue

FIFO structure. Enqueue at rear, dequeue at front. Deque supports these and reverse operations.

Expressiveness

Deque is more versatile: acts as stack, queue, or hybrid. Enables complex data flows and algorithms.

Performance

Similar time complexity for end operations. Deque may have slight overhead due to bidirectional pointers or circular indexing.

Algorithms Using Deques

Sliding Window Maximum

Maintains candidate maxima in deque for O(n) solution to maximum-in-subarrays problem.

Palindrome Detection

Uses deque to compare characters from both ends iteratively.

Cache Replacement Policies

Implements LRU or MRU via deque to track usage order.

BFS Variants

Double-ended queues used in 0-1 BFS to manage edge weights efficiently.

// Sliding Window Maximum Algorithm (pseudo-code)initialize deque D as emptyfor i in 0 to n-1: while D not empty and arr[D.back] <= arr[i]: D.pop_back() D.push_back(i) if D.front <= i - k: D.pop_front() if i >= k - 1: output arr[D.front]

Programming Language Support

C++ STL

std::deque container provides dynamic double-ended queue with random access, iterator support, and efficient end operations.

Java

java.util.Deque interface implemented by LinkedList and ArrayDeque. Supports stack and queue operations.

Python

collections.deque: thread-safe, memory efficient, supports O(1) appends and pops from both ends.

Other Languages

Available in JavaScript libraries, Rust’s std::collections::VecDeque, Go packages, and more.

Advantages and Limitations

Advantages

  • Flexible insertion/deletion at both ends.
  • Efficient O(1) time operations.
  • Supports multiple abstract data types.
  • Widely supported in standard libraries.

Limitations

  • Random access slower than arrays (except C++ deque).
  • Memory overhead in linked list implementations.
  • Dynamic resizing can cause performance hits.
  • Not suitable for priority-based retrieval.

References

  • Knuth, D. E. The Art of Computer Programming, Volume 1: Fundamental Algorithms. Addison-Wesley, 2011, pp. 233-240.
  • Tarjan, R. E. Data Structures and Network Algorithms. SIAM, 1983, pp. 45-55.
  • Cormen, T. H., Leiserson, C. E., Rivest, R. L., Stein, C. Introduction to Algorithms. MIT Press, 2009, pp. 87-89.
  • Goodrich, M. T., Tamassia, R. Data Structures and Algorithms in Java. Wiley, 2014, pp. 123-130.
  • Sedgewick, R., Wayne, K. Algorithms. Addison-Wesley, 2011, pp. 145-150.