3.4 Discussion and Exercises

Both singly-linked and doubly-linked lists are established techniques, having been used in programs for over 40 years. They are discussed, for example, by Knuth [46, Sections 2.2.3-2.2.5]. Even the SEList data structure seems to be a well-known data structures exercise. The SEList is sometimes referred to as an unrolled linked list [67].

Another way to save space in a doubly-linked list is to use so-called XOR-lists. In an XOR-list, each node, $ \ensuremath{\ensuremath{\mathit{u}}}$, contains only one pointer, called $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{nextprev}}}$, that holds the bitwise exclusive-or of $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{prev}}}$ and $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{next}}}$. The list itself needs to store two pointers, one to the $ \ensuremath{\ensuremath{\mathit{dummy}}}$ node and one to $ \ensuremath{\ensuremath{\mathit{dummy}}.\ensuremath{\mathit{next}}}$ (the first node, or $ \ensuremath{\ensuremath{\mathit{dummy}}}$ if the list is empty). This technique uses the fact that, if we have pointers to $ \ensuremath{\ensuremath{\mathit{u}}}$ and $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{prev}}}$, then we can extract $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{next}}}$ using the formula

$\displaystyle \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\math...
...ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{nextprev}}}} \enspace .

(Here ^ computes the bitwise exclusive-or of its two arguments.) This technique complicates the code a little and is not possible in some languages, like Java and Python, that have garbage collection but gives a doubly-linked list implementation that requires only one pointer per node. See Sinha's magazine article [68] for a detailed discussion of XOR-lists.

Exercise 3..1   Why is it not possible to use a dummy node in an SLList to avoid all the special cases that occur in the operations $ \ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{pop}()}$, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{remove}()}$?

Exercise 3..2   Design and implement an SLList method, $ \ensuremath{\mathrm{second\_last}()}$, that returns the second-last element of an SLList. Do this without using the member variable, $ \ensuremath{\ensuremath{\mathit{n}}}$, that keeps track of the size of the list.

Exercise 3..3   Implement the List operations $ \ensuremath{\mathrm{get}(\ensuremath{\mathit{i}})}$, $ \ensuremath{\mathrm{set}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ on an SLList. Each of these operations should run in $ O(1+\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}})$ time.

Exercise 3..4   Design and implement an SLList method, $ \ensuremath{\mathrm{reverse}()}$ that reverses the order of elements in an SLList. This method should run in $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ time, should not use recursion, should not use any secondary data structures, and should not create any new nodes.

Exercise 3..5   Design and implement SLList and DLList methods called $ \ensuremath{\mathrm{check\_size}()}$. These methods walk through the list and count the number of nodes to see if this matches the value, $ \ensuremath{\ensuremath{\mathit{n}}}$, stored in the list. These methods return nothing, but throw an exception if the size they compute does not match the value of $ \ensuremath{\ensuremath{\mathit{n}}}$.

Exercise 3..6   Try to recreate the code for the $ \ensuremath{\mathrm{add\_before}(\ensuremath{\mathit{w}})}$ operation that creates a node, $ \ensuremath{\ensuremath{\mathit{u}}}$, and adds it in a DLList just before the node $ \ensuremath{\ensuremath{\mathit{w}}}$. Do not refer to this chapter. Even if your code does not exactly match the code given in this book it may still be correct. Test it and see if it works.

The next few exercises involve performing manipulations on DLLists. You should complete them without allocating any new nodes or temporary arrays. They can all be done only by changing the $ \ensuremath{\ensuremath{\mathit{prev}}}$ and $ \ensuremath{\ensuremath{\mathit{next}}}$ values of existing nodes.

Exercise 3..7   Write a DLList method $ \ensuremath{\mathrm{is\_palindrome}()}$ that returns $ \ensuremath{\ensuremath{\mathit{true}}}$ if the list is a palindrome, i.e., the element at position $ \ensuremath{\ensuremath{\mathit{i}}}$ is equal to the element at position $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-i-1$ for all $ i\in\{0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1\}$. Your code should run in $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ time.

Exercise 3..8   Implement a method $ \ensuremath{\mathrm{rotate}(\ensuremath{\mathit{r}})}$ that ``rotates'' a DLList so that list item $ \ensuremath{\ensuremath{\mathit{i}}}$ becomes list item $ (\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}+\ensuremath{\ensuremath{\ensuremath{\mathit{r}}}})\bmod \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$. This method should run in $ O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{r}}}},\ensuremath{\ensur...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{r}}}}\})$ time and should not modify any nodes in the list.

Exercise 3..9   Write a method, $ \ensuremath{\mathrm{truncate}(\ensuremath{\mathit{i}})}$, that truncates a DLList at position $ \ensuremath{\ensuremath{\mathit{i}}}$. After executing this method, the size of the list will be $ \ensuremath{\ensuremath{\mathit{i}}}$ and it should contain only the elements at indices $ 0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}-1$. The return value is another DLList that contains the elements at indices $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-1$. This method should run in $ O(\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\})$ time.

Exercise 3..10   Write a DLList method, $ \ensuremath{\mathrm{absorb}(\ensuremath{\mathit{l_2}})}$, that takes as an argument a DLList, $ \ensuremath{\ensuremath{\mathit{l_2}}}$, empties it and appends its contents, in order, to the receiver. For example, if $ \ensuremath{\ensuremath{\mathit{l_1}}}$ contains $ a,b,c$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$ contains $ d,e,f$, then after calling $ \ensuremath{\ensuremath{\mathit{l_1}}.\mathrm{absorb}(\ensuremath{\mathit{l_2}})}$, $ \ensuremath{\ensuremath{\mathit{l_1}}}$ will contain $ a,b,c,d,e,f$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$ will be empty.

Exercise 3..11   Write a method $ \ensuremath{\mathrm{deal}()}$ that removes all the elements with odd-numbered indices from a DLList and return a DLList containing these elements. For example, if $ \ensuremath{\ensuremath{\mathit{l_1}}}$, contains the elements $ a,b,c,d,e,f$, then after calling $ \ensuremath{\ensuremath{\mathit{l_1}}.\mathrm{deal}()}$, $ \ensuremath{\ensuremath{\mathit{l_1}}}$ should contain $ a,c,e$ and a list containing $ b,d,f$ should be returned.

Exercise 3..12   Write a method, $ \ensuremath{\mathrm{reverse}()}$, that reverses the order of elements in a DLList.

Exercise 3..13   This exercise walks you through an implementation of the merge-sort algorithm for sorting a DLList, as discussed in Section 11.1.1.
  1. Write a DLList method called $ \ensuremath{\mathrm{take\_first}(\ensuremath{\mathit{l_2}})}$. This method takes the first node from $ \ensuremath{\ensuremath{\mathit{l_2}}}$ and appends it to the the receiving list. This is equivalent to $ \ensuremath{\mathrm{add}(\mathrm{size}(),\ensuremath{\mathit{l_2}}.\mathrm{remove}(0)})$, except that it should not create a new node.
  2. Write a DLList static method, $ \ensuremath{\mathrm{merge}(\ensuremath{\mathit{l_1}},\ensuremath{\mathit{l_2}})}$, that takes two sorted lists $ \ensuremath{\ensuremath{\mathit{l_1}}}$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$, merges them, and returns a new sorted list containing the result. This causes $ \ensuremath{\ensuremath{\mathit{l_1}}}$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$ to be emptied in the proces. For example, if $ \ensuremath{\ensuremath{\mathit{l_1}}}$ contains $ a,c,d$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$ contains $ b,e,f$, then this method returns a new list containing $ a,b,c,d,e,f$.
  3. Write a DLList method $ \ensuremath{\mathrm{sort}()}$ that sorts the elements contained in the list using the merge sort algorithm. This recursive algorithm works in the following way:
    1. If the list contains 0 or 1 elements then there is nothing to do. Otherwise,
    2. Using the $ \ensuremath{\mathrm{truncate}(\mathrm{size}()/2})$ method, split the list into two lists of approximately equal length, $ \ensuremath{\ensuremath{\mathit{l_1}}}$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$;
    3. Recursively sort $ \ensuremath{\ensuremath{\mathit{l_1}}}$;
    4. Recursively sort $ \ensuremath{\ensuremath{\mathit{l_2}}}$; and, finally,
    5. Merge $ \ensuremath{\ensuremath{\mathit{l_1}}}$ and $ \ensuremath{\ensuremath{\mathit{l_2}}}$ into a single sorted list.

The next few exercises are more advanced and require a clear understanding of what happens to the minimum value stored in a Stack or Queue as items are added and removed.

Exercise 3..14   Design and implement a MinStack data structure that can store comparable elements and supports the stack operations $ \ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{pop}()}$, and $ \ensuremath{\mathrm{size}()}$, as well as the $ \ensuremath{\mathrm{min}()}$ operation, which returns the minimum value currently stored in the data structure. All operations should run in constant time.

Exercise 3..15   Design and implement a MinQueue data structure that can store comparable elements and supports the queue operations $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{remove}()}$, and $ \ensuremath{\mathrm{size}()}$, as well as the $ \ensuremath{\mathrm{min}()}$ operation, which returns the minimum value currently stored in the data structure. All operations should run in constant amortized time.

Exercise 3..16   Design and implement a MinDeque data structure that can store comparable elements and supports all the deque operations $ \ensuremath{\mathrm{add\_first}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{add\_last}(\ensuremath{\mathit{x}})}$ $ \ensuremath{\mathrm{remove\_first}()}$, $ \ensuremath{\mathrm{remove\_last}()}$ and $ \ensuremath{\mathrm{size}()}$, and the $ \ensuremath{\mathrm{min}()}$ operation, which returns the minimum value currently stored in the data structure. All operations should run in constant amortized time.

The next exercises are designed to test the reader's understanding of the implementation and analysis of the space-efficient SEList:

Exercise 3..17   Prove that, if an SEList is used like a Stack (so that the only modifications to the SEList are done using $ \ensuremath{\ensuremath{\mathrm{push}(\ensuremath{\mathit{x}})}}\equiv
\ensuremath{\ensuremath{\mathrm{add}(\mathrm{size}(),x})}$ and $ \ensuremath{\ensuremath{\mathrm{pop}()}}\equiv \ensuremath{\ensuremath{\mathrm{remove}(\mathrm{size}()-1})}$), then these operations run in constant amortized time, independent of the value of $ \ensuremath{\ensuremath{\mathit{b}}}$.

Exercise 3..18   Design and implement of a version of an SEList that supports all the Deque operations in constant amortized time per operation, independent of the value of $ \ensuremath{\ensuremath{\mathit{b}}}$.

Exercise 3..19   Explain how to use the bitwise exclusive-or operator, ^, to swap the values of two $ \ensuremath{\ensuremath{\mathit{int}}}$ variables without using a third variable.