Circular doubly linked lists

Chapter: Circular doubly linked lists

Begin again by copying all your ex3 code to a new folder ex4.

Instead of the next reference of the last node being to null we will make it refer to the first node. Similarly, instead of the previous reference of the first node being null we will make it refer to the last node. In this way, our doubly linked list becomes circular.

There is now no need for a tail reference, since it is easily obtained as head.previous().

The only changes you need to make will be to DoublyLinkedList.java. So copy all your ex3 folder to a new folder ex4. Here is how I started off my new DoublyLinkedList.java. For the rest, I started from scratch rather than try to reuse the old code.

You really really really need to draw pictures for this exercise. You also need to be very careful that your loops terminate. Node that if you have a loop like:

while(finger != null) {
  ...
  finger = finger.next();
}
your loop will never terminate now that the list is circular. To give you an idea how to proceed, here is the loop from my contains method:
	while (finger != head.previous() && !finger.value().equals(value)) { 
	    finger = finger.next(); 
	} 
At the end of this loop, the finger will be pointed at the last element in the list and you can simply return finger.value().equals(value);.


Exercise 4

Finish the implementation of the Doubly Linked circular list.


rhyspj@gwu.edu