Circular linked lists
The last node links back to the first. Keep a stopping rule, such as counting visits or returning to the starting node.
Simple example
Follow four turns starting at 2. After 6 the link leads back to 2 again.
#include <stdbool.h>
#include <stdio.h>
int main(void) {
int values[] = {2, 4, 6};
int following[] = {1, 2, 0};
int node = 0;
int turn = 0;
while (turn < 4) {
printf("%d\n", values[node]);
node = following[node];
turn += 1;
}
}
2 4 6 2
Everyday examples
Rotate class helpers
Helpers A, B and C take turns. A starts. Who takes the fourth turn?
Three turns complete one trip around the ring. The next link returns to A.
Ride a circular shuttle
The stops are 4, 9 and 12. You board at 12. Which stop is next?
The last stop is linked to the first. You reach stop 4 without reversing direction.
Repeat a slideshow once
Slides 7, 3 and 8 repeat. Which slides should a one-cycle preview show if it starts at 3?
Count three slides and stop. Continuing until there is no next slide would keep the preview running.
Cost and limits
A ring has no final empty link. Following next until it is empty would never finish in a nonempty ring.