DLL Operation: Get

  • Trace the basic operations of a (doubly) linked-list implementation.
  • Understand the basic operations of a (doubly) linked list well enough to implement them.

Suppose we have a DLL with $n$ elements (nodes), and we want to get the data stored in the $k^{th}$ node (at index $k-1$).

Exercise Complete the implementation of the get method which returns data stored at a given index.

public T get(int index) {
  return null; // TODO Implement me!
}

Hint: you cannot directly jump to $K^{th}$ node. You need to start from the head or the tail and follow the pointers to get there!

For this exercise, traverse the list from the tail pointer. Assume there is a numElements attribute that correctly represents the number of nodes in this DLL.

Solution
public T get(int index) {
  Node<T> target = tail;
  for (int i = numElements - 1; i > index; i--) {
    target = target.prev;
  }
  return target.data;
}

Caution: the implementation above fails to account for an edge case!