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”/]

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20demonstrate%20circular%20linked%20list%20traversal%20%0A%20%0A%23%20Structure%20for%20a%20Node%0Aclass%20Node%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20create%20%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%0A%20%0Aclass%20CircularLinkedList%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20empty%20circular%20linked%20list%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20Function%20to%20insert%20a%20node%20at%20the%20beginning%20of%20a%0A%20%20%20%20%23%20circular%20linked%20list%0A%20%20%20%20def%20push(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20ptr1%20%3D%20Node(data)%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20ptr1.next%20%3D%20self.head%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20linked%20list%20is%20not%20None%20then%20set%20the%20next%20of%0A%20%20%20%20%20%20%20%20%23%20last%20node%0A%20%20%20%20%20%20%20%20if%20self.head%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20while(temp.next%20!%3D%20self.head)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%20%20%20%20%20%20%20%20%20%20%20temp.next%20%3D%20ptr1%0A%20%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ptr1.next%20%3D%20ptr1%20%23%20For%20the%20first%20node%0A%20%0A%20%20%20%20%20%20%20%20self.head%20%3D%20ptr1%20%0A%20%0A%20%20%20%20%23%20Function%20to%20print%20nodes%20in%20a%20given%20circular%20linked%20list%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20if%20self.head%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20while(True)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print%20%22%25d%22%20%25(temp.data)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(temp%20%3D%3D%20self.head)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0A%20%0A%23%20Initialize%20list%20as%20empty%0Acllist%20%3D%20CircularLinkedList()%0A%20%0A%23%20Created%20linked%20list%20will%20be%2011-%3E2-%3E56-%3E12%0Acllist.push(12)%0Acllist.push(56)%0Acllist.push(2)%0Acllist.push(11)%0A%20%0Aprint%20%22Contents%20of%20circular%20Linked%20List%22%0Acllist.printList()%0A” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

Contents of Circular Linked List
 11 2 56 12