In a circular linked list there is no end: the last node points back to the first. This suits data that is naturally processed in rounds, such as players taking turns or processes sharing a CPU. The circle also removes the NULL that tells a loop where to stop, so traversal needs a different stopping rule.
The structure
It is convenient to keep a pointer to the last node, last. Then the first node is last->next, so insertion at either end is O(1) with a single pointer.
A loop that waits for NULL would never stop. Instead, start at the first node and stop when you come back to it. A do … while loop visits the first node before testing:
Traversal of a circular list
if (last != NULL) { Node *p = last->next; /* first node */ do { visit(p->data); p = p->next; } while (p != last->next); /* back at the start: stop */}
Insertion and deletion
Insert at the front:n->next = last->next; last->next = n; (last does not change).
Insert at the end: the same two lines, then last = n.
Empty list: the new node points to itself: n->next = n; last = n;.
Delete a node: find its predecessor prev (going round at most once), set prev->next = p->next, and if p was the last node set last = prev; if p was the only node, the list becomes empty (last = NULL).
Uses of circular lists
Round-robin scheduling: the operating system gives each process a time slice in turn, going round the circle of ready processes.
Turn-based games and repeating playlists, where after the last item comes the first again.
Circular buffers for streaming data (often in an array, as in Unit 5’s circular queue).
Key takeaways
The last node points to the first; the empty list is last == NULL (or head == NULL).
A pointer to the last node gives O(1) access to both ends.
Traverse with do … while (p != first); never wait for NULL.
When the first node changes, also update last->next.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Can a doubly linked list be made circular? Explain, and give two differences between a doubly linked list and a circular linked list (Check Your Progress, Q2 and Q3).
Show worked answer
Yes. In a circular doubly linked list the right link of the last node points to the first node and the left link of the first node points to the last node, so there is no NULL and the list can be walked round in both directions. Differences between an ordinary doubly linked list and a (singly) circular linked list: (1) a doubly linked node has two links (left and right), a circular singly linked node has one; (2) a doubly linked list has NULL at both ends, a circular list has no NULL because the last node points to the first; (3) a doubly linked list can be traversed backwards, a singly circular list only forwards (but it can start anywhere and go round); (4) traversal stops at NULL in a doubly linked list, but when it returns to the start node in a circular list.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/lists/circular-linked-lists/ to read this page.