{"id":26879,"date":"2017-12-26T20:28:27","date_gmt":"2017-12-26T14:58:27","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26879"},"modified":"2017-12-26T20:28:27","modified_gmt":"2017-12-26T14:58:27","slug":"python-algorithm-circular-linked-list-traversal","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-circular-linked-list-traversal\/","title":{"rendered":"Python algorithm-Circular Linked List | Set 2 (Traversal)"},"content":{"rendered":"<p>We have discussed <a title=\"Permanent link to Circular Linked List | Set 1 (Introduction and Applications)\" href=\"http:\/\/quiz.geeksforgeeks.org\/circular-linked-list\/\" rel=\"bookmark\">Circular Linked List Introduction and Applications,<\/a> in the previous post on Circular Linked List. In this post, traversal operation is discussed.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26862\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-1.png\" alt=\"Circular Linked List | Set 2 (Traversal)\" width=\"887\" height=\"187\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-1.png 887w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-1-300x63.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-1-768x162.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p>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.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Python Programming:<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Output:<\/p>\n<pre>Contents of Circular Linked List\r\n 11 2 56 12<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Circular Linked List (Traversal) &#8211; Circular Linked List In a conventional linked list, we traverse the list from the head node and stop the traversal.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79477,79476],"tags":[80116,80119,80096,80118,80117,80115,79561,83749,83746,83745,83747,83748,83743,83742,83744],"class_list":["post-26879","post","type-post","status-publish","format-standard","hentry","category-circular-linked-list","category-linked-list","tag-algorithm-for-circular-linked-list-in-data-structure","tag-circular-doubly-linked-list-java-code","tag-circular-linked-list-algorithm-pdf","tag-circular-linked-list-applications-circular-linked-list-algorithm-ppt","tag-circular-linked-list-in-c","tag-circular-linked-list-insertion-and-deletion-program","tag-circular-linked-list-java","tag-linked-list-traversal-c","tag-searching-a-linked-list","tag-singly-linked-list-algorithm-data-structures","tag-traversal-linked-list-algorithm","tag-traverse-linked-list-means","tag-traversing-a-linked-list-algorithm","tag-traversing-a-linked-list-java","tag-traversing-a-linked-list-program-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26879"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26879\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}