We have discussed Circular Linked List Introduction and Applications, in the previous post on Circular Linked List. In this post, traversal operation is discussed.

Circular Linked List | Set 2 (Traversal)

In a conventional linked list, we traverse the list from the head node and stop the traversal when we reach NULL. In a circular linked list, we stop traversal when we reach the first node again. Following is C code for linked list traversal.

[pastacode lang=”c” manual=”%2F*%20Function%20to%20traverse%20a%20given%20Circular%20linked%20list%20and%20print%20nodes%20*%2F%0Avoid%20printList(struct%20node%20*first)%0A%7B%0A%20%20%20%20struct%20node%20*temp%20%3D%20first%3B%20%0A%20%0A%20%20%20%20%2F%2F%20If%20linked%20list%20is%20not%20empty%0A%20%20%20%20if%20(first%20!%3D%20NULL)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Keep%20printing%20nodes%20till%20we%20reach%20the%20first%20node%20again%0A%20%20%20%20%20%20%20%20do%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20temp-%3Edata)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20while%20(temp%20!%3D%20first)%3B%0A%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Complete C program to demonstrate traversal. Following is complete C program to demonstrate traversal of circular linked list.

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20structure%20for%20a%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20insert%20a%20node%20at%20the%20begining%20of%20a%20Circular%0A%20%20%20linked%20list%20*%2F%0Avoid%20push(struct%20node%20**head_ref%2C%20int%20data)%0A%7B%0A%20%20%20%20struct%20node%20*ptr1%20%3D%20(struct%20node%20*)malloc(sizeof(struct%20node))%3B%0A%20%20%20%20struct%20node%20*temp%20%3D%20*head_ref%3B%0A%20%20%20%20ptr1-%3Edata%20%3D%20data%3B%0A%20%20%20%20ptr1-%3Enext%20%3D%20*head_ref%3B%0A%20%0A%20%20%20%20%2F*%20If%20linked%20list%20is%20not%20NULL%20then%20set%20the%20next%20of%20last%20node%20*%2F%0A%20%20%20%20if%20(*head_ref%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20while%20(temp-%3Enext%20!%3D%20*head_ref)%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%0A%20%20%20%20%20%20%20%20temp-%3Enext%20%3D%20ptr1%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20ptr1-%3Enext%20%3D%20ptr1%3B%20%2F*For%20the%20first%20node%20*%2F%0A%20%0A%20%20%20%20*head_ref%20%3D%20ptr1%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20Circular%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*head)%0A%7B%0A%20%20%20%20struct%20node%20*temp%20%3D%20head%3B%0A%20%20%20%20if%20(head%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20do%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20temp-%3Edata)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20while%20(temp%20!%3D%20head)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Initialize%20lists%20as%20empty%20*%2F%0A%20%20%20%20struct%20node%20*head%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20Created%20linked%20list%20will%20be%2011-%3E2-%3E56-%3E12%20*%2F%0A%20%20%20%20push(%26head%2C%2012)%3B%0A%20%20%20%20push(%26head%2C%2056)%3B%0A%20%20%20%20push(%26head%2C%202)%3B%0A%20%20%20%20push(%26head%2C%2011)%3B%0A%20%0A%20%20%20%20printf(%22Contents%20of%20Circular%20Linked%20List%5Cn%20%22)%3B%0A%20%20%20%20printList(head)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Contents of Circular Linked List
 11 2 56 12
[ad type=”banner”]