CSData StructuresDiagramBeginner
Deque (Double-Ended Queue)
KO: 덱 (양방향 큐)·EN: Deque (Double-Ended Queue)·JA: デック(両端キュー)·Double-Ended Queue
A generalized linear structure supporting O(1) insertion and removal at both front and back ends, unifying stacks and queues
Table of Contents
Structure Diagram
Deque (Double-Ended Queue) Front & Back O(1) OperationsTap a node
ROOTpushFront / popFront
1 linkQuick Answer
What is Deque (Double-Ended Queue)?
A generalized linear structure supporting O(1) insertion and removal at both front and back ends, unifying stacks and queues. It is a core concept in Data Structures within CS.
Core Principles
A Deque (Double-Ended Queue) generalizes both stacks and queues by allowing O(1) insertion and deletion at both the front and the back.
- Under the Hood: Rather than plain linked lists, production deques use resizable circular arrays (Java
ArrayDeque, RustVecDeque) or arrays of fixed-size blocks (C++std::deque, Pythoncollections.deque) for strong CPU cache locality. - Unified Primitive: Push/pop on the same end for LIFO stack behavior, or opposite ends for FIFO queue behavior.
Practical Takeaways
- Monotonic Deque for Sliding Windows: Maintain elements in sorted order by popping obsolete indices from the front and dominated candidates from the back to compute window min/max in
O(N). - Avoid
O(N)front shifts: CallingArray.shift()in JS orlist.pop(0)in Python copies the entire array—use a deque whenever front removals happen in a loop. - Work-Stealing Schedulers: Worker threads push/pop their own tasks at the back (cache-hot LIFO), while idle workers steal older tasks from the front of other threads' deques.
Notable Examples
- Async Runtimes & Thread Pools: Go scheduler, Rust Tokio, and Java
ForkJoinPoolwork-stealing queues. - 0-1 BFS: Pushing 0-weight edges to the front and 1-weight edges to the back for
O(V + E)shortest paths. - Streaming Metrics: Tracking sliding-window max latency or peak throughput in real time.