{"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 noopener\" target=\"_blank\">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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* Function to traverse a given Circular linked list and print nodes *\/<br\/>void printList(struct node *first)<br\/>{<br\/>    struct node *temp = first; <br\/> <br\/>    \/\/ If linked list is not empty<br\/>    if (first != NULL) <br\/>    {<br\/>        \/\/ Keep printing nodes till we reach the first node again<br\/>        do<br\/>        {<br\/>            printf(&quot;%d &quot;, temp-&gt;data);<br\/>            temp = temp-&gt;next;<br\/>        }<br\/>        while (temp != first);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Complete C program to demonstrate traversal.<\/strong> Following is complete C program to demonstrate traversal of circular linked list.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* structure for a node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node *next;<br\/>};<br\/> <br\/>\/* Function to insert a node at the begining of a Circular<br\/>   linked list *\/<br\/>void push(struct node **head_ref, int data)<br\/>{<br\/>    struct node *ptr1 = (struct node *)malloc(sizeof(struct node));<br\/>    struct node *temp = *head_ref;<br\/>    ptr1-&gt;data = data;<br\/>    ptr1-&gt;next = *head_ref;<br\/> <br\/>    \/* If linked list is not NULL then set the next of last node *\/<br\/>    if (*head_ref != NULL)<br\/>    {<br\/>        while (temp-&gt;next != *head_ref)<br\/>            temp = temp-&gt;next;<br\/>        temp-&gt;next = ptr1;<br\/>    }<br\/>    else<br\/>        ptr1-&gt;next = ptr1; \/*For the first node *\/<br\/> <br\/>    *head_ref = ptr1;<br\/>}<br\/> <br\/>\/* Function to print nodes in a given Circular linked list *\/<br\/>void printList(struct node *head)<br\/>{<br\/>    struct node *temp = head;<br\/>    if (head != NULL)<br\/>    {<br\/>        do<br\/>        {<br\/>            printf(&quot;%d &quot;, temp-&gt;data);<br\/>            temp = temp-&gt;next;<br\/>        }<br\/>        while (temp != head);<br\/>    }<br\/>}<br\/> <br\/>\/* Driver program to test above functions *\/<br\/>int main()<br\/>{<br\/>    \/* Initialize lists as empty *\/<br\/>    struct node *head = NULL;<br\/> <br\/>    \/* Created linked list will be 11-&gt;2-&gt;56-&gt;12 *\/<br\/>    push(&amp;head, 12);<br\/>    push(&amp;head, 56);<br\/>    push(&amp;head, 2);<br\/>    push(&amp;head, 11);<br\/> <br\/>    printf(&quot;Contents of Circular Linked List\\n &quot;);<br\/>    printList(head);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Contents of Circular Linked List\r\n 11 2 56 12<\/pre>\n[ad type=&#8221;banner&#8221;]\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}]}}