Deque (ADT)

From Notes
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:


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 StackQueueDouble-Ended Queue (Deque)
Random Access to Elements VectorListSequence
Key/Value Pairs TreeBinary TreePriority QueueHeapDictionarySkip List
Sorted Storage Binary Search TreeAVL TreeRed-Black Tree