{"id":26870,"date":"2017-12-26T20:38:35","date_gmt":"2017-12-26T15:08:35","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26870"},"modified":"2017-12-26T20:38:35","modified_gmt":"2017-12-26T15:08:35","slug":"c-algorithm-find-length-linked-list-iterative-recursive-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-find-length-linked-list-iterative-recursive-2\/","title":{"rendered":"Cpp Algorithm &#8211; Find Length of a Linked List both Iterative and Recursive"},"content":{"rendered":"<p>Write a C function to count number of nodes in a given singly linked list.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26889\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length.png\" alt=\"C Algorithm - Find Length of a Linked List both Iterative and Recursive\" width=\"878\" height=\"184\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length.png 878w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length-300x63.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length-768x161.png 768w\" sizes=\"(max-width: 878px) 100vw, 878px\" \/><\/p>\n<p>For example, the function should return 5 for linked list 1->3->1->2->1.<\/p>\n<p><strong>Iterative Solution<\/strong><\/p>\n<pre>1) Initialize count as 0 \r\n2) Initialize a node pointer, current = head.\r\n3) Do following while current is not NULL\r\n     a) current = current -> next\r\n     b) count++;\r\n4) Return count<\/pre>\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20Iterative%20C%20program%20to%20find%20length%20or%20count%20of%20nodes%20in%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20of%20a%20list%20and%20an%20int%2C%20push%20a%20new%20node%20on%20the%20front%0A%20%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Counts%20no.%20of%20nodes%20in%20linked%20list%20*%2F%0Aint%20getCount(struct%20node*%20head)%0A%7B%0A%20%20%20%20int%20count%20%3D%200%3B%20%20%2F%2F%20Initialize%20count%0A%20%20%20%20struct%20node*%20current%20%3D%20head%3B%20%20%2F%2F%20Initialize%20current%0A%20%20%20%20while%20(current%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20count%2B%2B%3B%0A%20%20%20%20%20%20%20%20current%20%3D%20current-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20count%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20count%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20Use%20push()%20to%20construct%20below%20list%0A%20%20%20%20%201-%3E2-%3E1-%3E3-%3E1%20%20*%2F%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%203)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%202)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%0A%20%20%20%20%2F*%20Check%20the%20count%20function%20*%2F%0A%20%20%20%20printf(%22count%20of%20nodes%20is%20%25d%22%2C%20getCount(head))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>Output:<\/strong><\/p>\n<pre>count of nodes is 5<\/pre>\n<p><strong>Recursive Solution<\/strong><\/p>\n<pre><strong>int getCount(head)<\/strong>\r\n1) If head is NULL, return 0.\r\n2) Else return 1 + getCount(head->next)<\/pre>\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20Recursive%20C%20program%20to%20find%20length%20or%20count%20of%20nodes%20in%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20of%20a%20list%20and%20an%20int%2C%20push%20a%20new%20node%20on%20the%20front%0A%20%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Counts%20the%20no.%20of%20occurences%20of%20a%20node%0A%20%20%20(search_for)%20in%20a%20linked%20list%20(head)*%2F%0Aint%20getCount(struct%20node*%20head)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%0A%20%20%20%20if%20(head%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20count%20is%201%20%2B%20count%20of%20remaining%20list%0A%20%20%20%20return%201%20%2B%20getCount(head-%3Enext)%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20count%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20Use%20push()%20to%20construct%20below%20list%0A%20%20%20%20%201-%3E2-%3E1-%3E3-%3E1%20%20*%2F%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%203)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%202)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%0A%20%20%20%20%2F*%20Check%20the%20count%20function%20*%2F%0A%20%20%20%20printf(%22count%20of%20nodes%20is%20%25d%22%2C%20getCount(head))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>Output:<\/strong><\/p>\n<pre>count of nodes is 5<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ Algorithm &#8211; Find Length of a Linked List both Iterative and Recursive &#8211; Linked List &#8211; Write a C function to count number of nodes in a given singly<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,79476,79478],"tags":[72483,80240,80236,80239,80231,80237,80241,80232,80238,80235,80234,80230,80233],"class_list":["post-26870","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-linked-list","category-singly-linked-list","tag-c-linked-list","tag-find-length-of-linked-list-java","tag-geeksforgeeks","tag-get-length-of-linked-list-python","tag-how-to-calculate-the-length-of-linked-list-without-using-counter-in-java","tag-how-to-find-the-length-of-a-linked-list-python","tag-length-of-linked-list-c","tag-length-of-linked-list-python","tag-linked-list-implementation-in-python","tag-linkedlist-java","tag-recursive-linked-list-java","tag-size-of-linked-list-c","tag-what-is-a-class-loader-what-does-it-do"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26870","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=26870"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26870\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}