Summary
We looked at another common data structure called a deque. A deque is a double-ended queue, so insertions and deletions are possible at both ends. We looked at pushFront
, popFront
, pushBack
, and popBack
operations. We also looked at some applications of deques.
Deques have a simple implementation in the imperative world. A doubly linked list could be used for implementing it. A vector could be used too.
We looked at two important concepts: amortization and lazy evaluation. We took a detailed look at Scala's Streams. We saw what a thunk is and how it helps us with delayed evaluation.
We then used all these concepts to check whether we can create more efficient queues. As we saw in the previous chapter, reversing an in
list and appending it to the out
list could be costly.
Then we applied the same concepts to look at how to implement persistent and immutable deques. We also saw how balancing a data structure plays a vital role in improving performance.
With all this knowhow, in the...