Circular Linked List

Chapter: Circular Linked List

My implementation of a Circular Linked List uses a plain linked structure inside of it.

Here is the code I used for the linked structure. Notice that because we want to build a circular structure out of these Nodes I don't really need a distinct ENode class, because once there is at least one node in the structure, there is no longer any need for a reference to an empty node. Any code that is traversing a list of Nodes will never encounter an empty node.

As I mention in the documentation, you may want to hide this class by defining it as an inner class inside the CircularList class that's coming soon. I deliberately therefore avoided declaring the class public.

The picture shows a circular linked list with two handles, one called head to the "first" node (whatever "first" may mean) and another called tail directly referring to the "last" (whatever "last" may mean) node.

Q. 3
Do we really need two handles?


The fewer handles we have, the less work will be involved in any of our operations. We do not need both tail and head.

Q. 4
So which shall we keep?


Here is my code for CircularList, except that you have to write your own rotate() method.

Notice that it is all pretty straightforward. You don't have fiddly special cases as you do at the beginning and end of linear linked lists. The only remaining tricky thing is dealing with the empty list.


Exercise 1

Finish the code by writing the rotate() method. Here is a class for testing your resulting circular list. If you have correctly implemented the rotate() method, then your program should behave like this:
java TestList one two three
one
two
three
one
two
three
one
two
three



rhyspj@gwu.edu