The Deque
This chapter covers the Deque ADT, a collection where a client can add, remove, and look at items at either end. We implement it twice, as LinkedDeque built on a doubly linked list and as ArrayDeque built on a circular array, and compare the two.
After reading this chapter, you should be able to:
- Describe the Deque ADT: the
addFirst,addLast,removeFirst,removeLast,getFirst,getLast, andisEmptyoperations, the two-ended access pattern, and the documented exception behavior - Explain how a deque can be used as a stack or as a queue, depending on which ends the client uses
- Explain why a deque needs a doubly linked list where a singly linked list was enough for a queue
- Implement and trace
LinkedDeque, a doubly linked list with a sentinel node at each end - Implement and trace
ArrayDeque, a circular array whosefrontindex moves in both directions - Compare the two implementations in time and memory
You can download the accompanying code for this chapter: starter and solution.
Sections
- Double-Ended Queue (Deque)
- The Deque ADT
- Deque ADT Specification
- Using a Linked List to Implement a Deque
- The LinkedDeque Class
- Implementing addFirst and addLast
- Implementing getFirst, getLast, removeFirst, and removeLast
- Tracing the LinkedDeque
- Using an Array to Implement a Deque
- Designing the ArrayDeque
- The ArrayDeque Class
- Implementing addFirst, addLast, and grow
- Tracing the ArrayDeque
- Two Deques, Side by Side