{"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-&gt;3-&gt;1-&gt;2-&gt;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 -&gt; next\r\n     b) count++;\r\n4) Return count<\/pre>\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Iterative C program to find length or count of nodes in a linked list<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* Link list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* next;<br\/>};<br\/> <br\/>\/* Given a reference (pointer to pointer) to the head<br\/>  of a list and an int, push a new node on the front<br\/>  of the list. *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>            (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* link the old list off the new node *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/> <br\/>    \/* move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/* Counts no. of nodes in linked list *\/<br\/>int getCount(struct node* head)<br\/>{<br\/>    int count = 0;  \/\/ Initialize count<br\/>    struct node* current = head;  \/\/ Initialize current<br\/>    while (current != NULL)<br\/>    {<br\/>        count++;<br\/>        current = current-&gt;next;<br\/>    }<br\/>    return count;<br\/>}<br\/> <br\/>\/* Drier program to test count function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/> <br\/>    \/* Use push() to construct below list<br\/>     1-&gt;2-&gt;1-&gt;3-&gt;1  *\/<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 3);<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 2);<br\/>    push(&amp;head, 1);<br\/> <br\/>    \/* Check the count function *\/<br\/>    printf(&quot;count of nodes is %d&quot;, getCount(head));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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-&gt;next)<\/pre>\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Recursive C program to find length or count of nodes in a linked list<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* Link list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* next;<br\/>};<br\/> <br\/>\/* Given a reference (pointer to pointer) to the head<br\/>  of a list and an int, push a new node on the front<br\/>  of the list. *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>            (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* link the old list off the new node *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/> <br\/>    \/* move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/* Counts the no. of occurences of a node<br\/>   (search_for) in a linked list (head)*\/<br\/>int getCount(struct node* head)<br\/>{<br\/>    \/\/ Base case<br\/>    if (head == NULL)<br\/>        return 0;<br\/> <br\/>    \/\/ count is 1 + count of remaining list<br\/>    return 1 + getCount(head-&gt;next);<br\/>}<br\/> <br\/>\/* Drier program to test count function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/> <br\/>    \/* Use push() to construct below list<br\/>     1-&gt;2-&gt;1-&gt;3-&gt;1  *\/<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 3);<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 2);<br\/>    push(&amp;head, 1);<br\/> <br\/>    \/* Check the count function *\/<br\/>    printf(&quot;count of nodes is %d&quot;, getCount(head));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}