liveBook · Manning

Circular linked list

As explained in Grokking Data Structures

A circular linked list is a linked list whose final node links back to a node in the list, forming a cycle rather than ending at a null reference.

A circular linked list is a linked list designed to keep going. Instead of ending with a null reference, the tail node points back to the head, forming a cycle. This makes it useful when items, resources, or tasks must repeat rather than stop. section 6.21 section 6.24

Use the linked-node diagram to trace how the tail returns to the head instead of reaching an endpoint. section 6.22

Choose the link direction according to how the cycle must be traversed:

  • Use a circular singly linked list when traversal only moves forward, such as through a slideshow, agricultural cycle, or server list. section 6.22
  • Use a circular doubly linked list when traversal must move forward and backward, such as through a carousel or revisable process. section 6.22
  • Stop traversal deliberately: because every node has a successor, an ordinary loop can continue forever. section 6.23
  • For a circular doubly linked list, the tail can be reached as head.prev; step-by-step traversal can track a current node. section 6.23

A repeating slideshow

A slideshow that displays images in order and starts again after the final image needs only forward movement, so a circular singly linked list fits. A carousel that lets users move backward as well as forward needs the doubly linked version. section 6.22

Implementation requires only small changes to a regular linked-list design, but insertion and deletion must preserve the cycle. If step-by-step traversal is supported, updates must also keep the current-traversal pointer correct. Earlier linked-list classes may be reusable through composition or inheritance. section 6.23

How Grokking Data Structures covers it

How deep it goes
Defines the tail-to-head cycle, compares singly and doubly linked variants, and gives implementation cautions without detailed code.
Examples
Uses slideshows, carousels, agricultural cycles, water cycles, startup phases, cache nodes, and server routing as repeating examples.
What it assumes
Assumes linked-list operations and node links, while distinguishing one-way from two-way traversal through practical examples.
What you can do after
Build it from scratch by adapting a singly or doubly linked list and protecting traversal and current-node pointers from invalid updates.
How it connects
Connects circular linked lists with linked lists, singly linked lists, doubly linked lists, traversal, insertion, deletion, and dynamic collections.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.