chapter three
3 An immutable deque
This chapter covers
The interface of an immutable double-ended queue, or deque, is a straightforward extension of the single-ended queue data type, but finding a data structure that lets us cheaply add and remove items from both ends of the list isn’t easy. The naïve approach of building a linked list that can be linked in two directions is tempting, but we’ll see that its performance is poor. Also, it would be nice if we could somehow reduce the O(n) worst case for dequeuing, which our queue implementation in chapter 2 demonstrates. In short, we have to be smarter about choosing a data structure to implement these abstract data types. The data structure we’ll use in this chapter is considerably more complex than any we’ve seen so far.