Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Circular Linked List

Data Structures - Circular Linked List

A circular linked list is a variant of the linked list structure; its core feature is that the last node no longer points to NULL but instead points back to the first node, forming a ring-shaped structure logically.


Structural characteristics of a circular linked list

Circular linked list - singly circular and doubly circular

Singly circular linked list

10

→

20

→

30

↺ tail.next = head (back to start)

Each node has only a next pointer; the tail node points to the head node, forming a closed loop

Doubly circular linked list

10

⇄

20

⇄

30

↺ head.prev = tail, tail.next = head

Each node has prev + next; the head and tail interconnect to form a double loop

Ordinary linked list: tail next = NULL, traversal termination condition is cur == NULL

Circular linked list: tail next = head, traversal termination condition is cur == head (back to start)

**Common operations:** Insert head node O(n) - must find the tail node first | Traversal uses do...while loop

Insert tail node O(1) (maintain tail pointer) | deletion is similar

Circular linked lists are mainly divided into two types:

The biggest feature of the circular linked list structure is that it natively supports continuous traversal from tail back to head.

The condition for determining the end of traversal is not 'whether NULL is reached' but 'whether the starting node has been returned to'.


Basic operations

Example

#include

#include

struct

Node

{

   

int

data

;

   

struct

Node

next

;

}

;

/* Create a new node */

struct

Node

createNode

(

int

value

)

{

   

struct

Node

newNode

=

(

struct

Node

)

malloc

(

sizeof

(

struct

Node

)

)

;

    newNode

->

data

=

value

;

    newNode

->

next

=

newNode

;

 

/* For a single node, next points to itself, forming its own loop */

   

return

newNode

;

}

/* Insert a node at the head of the circular linked list
   Time complexity: O(n) - need to find the tail node first to update its next pointer */

struct

Node

insertAtHead

(

struct

Node

head

,

int

value

)

{

   

struct

Node

newNode

=

createNode

(

value

)

;

   

if

(

head

==

NULL

)

{

       

return

newNode

;

 

/* Empty list: the new node forms its own loop */

   

}

   

/* Find the tail node (the node whose next points to head) */

   

struct

Node

tail

=

head

;

   

while

(

tail

->

next

!=

head

)

{

        tail

=

tail

->

next

;

   

}

    newNode

->

next

=

head

;

 

/* The new node's next points to the original head node */

    tail

->

next

=

newNode

;

 

/* The tail node's next points to the new node (the new head) */

   

return

newNode

;

         

/* The new node becomes the new head node */

}

/* Traverse the circular linked list (one full loop)
   Start from the head node and stop when you return to the head node again */

void

traverse

(

struct

Node

head

)

{

   

if

(

head

==

NULL

)

return

;

   

struct

Node

cur

=

head

;

   

printf

(

Circular Linked List:

)

;

   

do

{

       

printf

(

"%d -> "

,

cur

->

data

)

;

        cur

=

cur

->

next

;

   

}

while

(

cur

!=

head

)

;

 

/* stop when returning to the starting point */

   

printf

(

"(back to start)\n"

)

;

}

int

main

(

)

{

   

struct

Node

head

=

NULL

;

    head

=

insertAtHead

(

head

,

30

)

;

    head

=

insertAtHead

(

head

,

20

)

;

    head

=

insertAtHead

(

head

,

10

)

;

    traverse

(

head

)

;

   

/* output: Circular Linked List: 10 -> 20 -> 30 -> (back to start) */

   

return

0

;

}

The key to traversing a circular linked list is to use do...while a statement instead of while. Because using while (cur != head) , the initial state cur already equals head, so the loop would never execute. Whereas do...while guarantees the loop body executes at least once before checking the termination condition.


Classic application: the Josephus Circle problem

Josephus Circle problem (n=7, m=3)

Initial state: 7 people form a circle, starting from

1

start counting; every 3rd person counted is eliminated

1

at the beginning

2

3

Round 1

4

5

6

7

Counting direction 1, 2, 3, ...

Elimination order and final result (n=7, m=3)

3

Round 1

→

6

Round 2

→

2

Round 3

→

7

Round 4

→

5

Round 5

→

1

Round 6

→

4

Survivor

The Josephus problem (Josephus Problem) is a classic mathematical problem:

Suppose n people form a circle and start counting from the first person; every m-th person counted is eliminated from the circle, then counting restarts from the next person, repeating in a loop until only one person remains.

This problem naturally fits the structural property of a circular linked list, which is "head and tail connected, circularly accessible".

Example

#include

#include

struct

Node

{

   

int

data

;

         

/* person's number */

   

struct

Node

next

;

}

;

/* solving the Josephus circle
   parameters: n total number of people, m the person counted to m exits the circle
   simulate the whole process using a circular linked list */

void

josephus

(

int

n

,

int

m

)

{

   

if

(

n

<=

0

)

return

;

   

/* step 1: create a circular linked list containing n people (numbered 1~n) */

   

struct

Node

head

=

(

struct

Node

)

malloc

(

sizeof

(

struct

Node

)

)

;

    head

->

data

=

1

;

    head

->

next

=

head

;

 

/* when there is only one person, self-loop */

   

struct

Node

tail

=

head

;

   

for

(

int

i

=

2

;

i

<=

n

;

i

++

)

{

       

struct

Node

newNode

=

(

struct

Node

)

malloc

(

sizeof

(

struct

Node

)

)

;

        newNode

->

data

=

i

;

        newNode

->

next

=

head

;

 

/* the new node's next always points to the head */

        tail

->

next

=

newNode

;

 

/* the previous node's next points to the new node */

        tail

=

newNode

;

         

/* tail advances */

   

}

   

/* step 2: simulate the elimination process */

   

struct

Node

cur

=

head

;

   

struct

Node

prev

=

tail

;

 

/* prev points to cur's predecessor, initially the tail node */

   

printf

(

"Josephus Circle (n=%d, m=%d) elimination order: "

,

n

,

m

)

;

   

while

(

cur

->

next

!=

cur

)

{

 

/* stop when only one node remains (self-loop) */

       

/* counting: count m-1 times, advancing both prev and cur */

       

for

(

int

count

=

1

;

count

<

m

;

count

++

)

{

            prev

=

cur

;

            cur

=

cur

->

next

;

       

}

       

/* when counting reaches m, cur is eliminated */

       

printf

(

"%d "

,

cur

->

data

)

;

        prev

->

next

=

cur

->

next

;

/* skip cur */

       

free

(

cur

)

;

             

/* free the eliminated node */

        cur

=

prev

->

next

;

       

/* restart counting from the next person */

   

}

   

printf

(

"=> final survivor: %d\n"

,

cur

->

data

)

;

 

/* output the last person */

   

free

(

cur

)

;

}

int

main

(

)

{

    josephus

(

7

,

3

)

;

   

/* output: Josephus Circle (n=7, m=3) elimination order: 3 6 2 7 5 1 => final survivor: 4 */

   

return

0

;

}


Other application scenarios

Scenario Description
Operating system time-slice round-robin scheduling The CPU allocates time slices to each process in turn; when one process finishes its slice, the next one takes its turn, forming a cycle
Turn-based card playing in multiplayer games In card and board games, players take turns in clockwise (or counterclockwise) order
Circular buffer Circular buffer for audio/video data streams, which wraps back to the beginning and overwrites old data once full