Deque (ADT)
Jump to navigation
Jump to search
A double-ended queue or deque is simply a combination of a stack and a queue in that items can be inserted or removed from both ends.
Functions
- int size() — Returns how many items are in the deque.
- bool isEmpty() — Returns whether the deque is empty (i.e. size is 0).
- void insertFirst(Object o) — Puts an object at the front.
- void insertLast(Object o) — Puts an object at the back.
- Object removeFirst() — Removes the object from the front and returns it.
- Object removeLast() — Removes the object from the back and returns it.
- Object first() — Peeks at the front item without removing it.
- Object last() — Peeks at the back item without removing it.
Implementations
Identical to queue's implementations:
- Fixed/Circular Array
- Growable (Doubling) Array
- Singly-Linked-List. For analysis purposes, we'll assume that the front is the head of the list and the back is the tail of the list.
Analysis
See Amortized Time and Asymptotic Analysis for more information.
Array (fixed) | Array (doubling) | List | |||
---|---|---|---|---|---|
insertFirst() | worst: | ||||
best: | |||||
avg: | |||||
insertLast() | worst: | ||||
best: | |||||
avg: | |||||
removeFirst() | |||||
removeLast() |
[ template ] Abstract Data Types (ADTs) | |
---|---|
Ordered Access to Elements | Stack • Queue • Double-Ended Queue (Deque) |
Random Access to Elements | Vector • List • Sequence |
Key/Value Pairs | Tree • Binary Tree • Priority Queue • Heap • Dictionary • Skip List |
Sorted Storage | Binary Search Tree • AVL Tree • Red-Black Tree |