3. Circular Lists
A circular list is one in which the last node is followed by
the first node:
Circular lists are usually preferable to non-circular ones, for two
reasons:
- They provide easy access to both ends of a list: notice that we
do not need the
first
pointer in the header,
because we can easily reach the first node with
L->last->next
- the code for processing a circular list is often simpler
than the code for processing a non-circular list - mainly
because you don't have to constantly check if the next
node is defined - it is always defined.
At the implementation level, however, circularity does create one very
tricky case that is a very common source of bugs, and that is singleton
lists. When there is only one node in a circular list, it
points to itself as the next node. You have to think
carefully to handle this correctly.