{"id":27086,"date":"2018-01-02T22:13:16","date_gmt":"2018-01-02T16:43:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27086"},"modified":"2018-01-02T22:13:16","modified_gmt":"2018-01-02T16:43:16","slug":"c-algorithm-write-function-counts-number-times-given-int-occurs-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-write-function-counts-number-times-given-int-occurs-linked-list\/","title":{"rendered":"C Algorithm &#8211; Write a function that counts the number of times a given int occurs in a Linked List"},"content":{"rendered":"<p>Given a singly linked list and a key, count number of occurrences of given key in linked list. For example, if given linked list is 1-&gt;2-&gt;1-&gt;2-&gt;1-&gt;3-&gt;1 and given key is 1, then output should be 4.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre>1. Initialize count as zero.\r\n2. Loop through each element of linked list:\r\n     a) If element data is equal to the passed number then\r\n        increment the count.\r\n3. Return count. \r\n<\/pre>\n<p><strong>C Programming:<\/strong><\/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\">\/\/ C\/C++ program to count occurrences 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 count(struct node* head, int search_for)<br\/>{<br\/>    struct node* current = head;<br\/>    int count = 0;<br\/>    while (current != NULL)<br\/>    {<br\/>        if (current-&gt;data == search_for)<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 1 is %d&quot;, count(head, 1));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>count of 1 is 3<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<br \/>\n<strong>Auxiliary Space:<\/strong> O(1)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Write a function that counts the number of times a given int occurs in a Linked List &#8211; Linked List &#8211; Given a singly linked list and a key <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,79476,79478],"tags":[80781,80785,80783,80788,80240,80782,80786,80787,80790,80789,80784],"class_list":["post-27086","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-linked-list","category-singly-linked-list","tag-algorithm-to-count-the-number-of-nodes-in-linked-list","tag-c-program-to-count-repeated-number-in-an-array","tag-count-nodes-in-linked-list-c","tag-count-the-number-of-nodes-in-a-doubly-linked-list","tag-find-length-of-linked-list-java","tag-how-to-count-the-number-of-nodes-in-a-linked-list-java","tag-linked-list-count-java","tag-pseudo-code-to-delete-a-singly-linked-list","tag-pseudocode-to-delete-a-singly-linked-list","tag-write-a-program-to-count-the-number-of-times-an-item-is-present-in-a-linked-list","tag-write-a-python-program-that-accept-a-word-from-the-user-and-reverse-it"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27086","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=27086"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27086\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}