{"id":26859,"date":"2017-12-26T20:23:31","date_gmt":"2017-12-26T14:53:31","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26859"},"modified":"2017-12-26T20:23:31","modified_gmt":"2017-12-26T14:53:31","slug":"circular-linked-list-traversal","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/circular-linked-list-traversal\/","title":{"rendered":"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[ad type=\u201dbanner\u201d]\n<p><strong>Complete C program to demonstrate traversal.<\/strong> Following is complete C program to demonstrate traversal of circular linked list.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>Contents of Circular Linked List\r\n 11 2 56 12<\/pre>\n[ad type=\u201dbanner\u201d]\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,83741,80119,80096,80118,83738,83740,83739,80117,80115,79561,83734,83736,83735,83737],"class_list":["post-26859","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-algorithm-for-traversing-a-circular-linked-list","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-algorithm","tag-circular-linked-list-in-c-pdf","tag-circular-linked-list-in-c-program","tag-circular-linked-list-in-c","tag-circular-linked-list-insertion-and-deletion-program","tag-circular-linked-list-java","tag-circular-linked-list-traversal-algorithm","tag-search-an-item-from-circular-header-linked-list","tag-traversing-a-circular-linked-list-java","tag-write-an-algorithm-to-search-an-item-from-circular-header-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26859","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=26859"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26859\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}