{"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 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<p>Python Programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to demonstrate circular linked list traversal <br\/> <br\/># Structure for a Node<br\/>class Node:<br\/>     <br\/>    # Constructor to create  a new node<br\/>    def __init__(self, data):<br\/>        self.data = data <br\/>        self.next = None<br\/> <br\/>class CircularLinkedList:<br\/>     <br\/>    # Constructor to create a empty circular linked list<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/>    # Function to insert a node at the beginning of a<br\/>    # circular linked list<br\/>    def push(self, data):<br\/>        ptr1 = Node(data)<br\/>        temp = self.head<br\/>         <br\/>        ptr1.next = self.head<br\/> <br\/>        # If linked list is not None then set the next of<br\/>        # last node<br\/>        if self.head is not None:<br\/>            while(temp.next != self.head):<br\/>                temp = temp.next<br\/>            temp.next = ptr1<br\/> <br\/>        else:<br\/>            ptr1.next = ptr1 # For the first node<br\/> <br\/>        self.head = ptr1 <br\/> <br\/>    # Function to print nodes in a given circular linked list<br\/>    def printList(self):<br\/>        temp = self.head<br\/>        if self.head is not None:<br\/>            while(True):<br\/>                print &quot;%d&quot; %(temp.data),<br\/>                temp = temp.next<br\/>                if (temp == self.head):<br\/>                    break<br\/> <br\/> <br\/># Driver program to test above function<br\/> <br\/># Initialize list as empty<br\/>cllist = CircularLinkedList()<br\/> <br\/># Created linked list will be 11-&gt;2-&gt;56-&gt;12<br\/>cllist.push(12)<br\/>cllist.push(56)<br\/>cllist.push(2)<br\/>cllist.push(11)<br\/> <br\/>print &quot;Contents of circular Linked List&quot;<br\/>cllist.printList()<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}