{"id":26916,"date":"2017-12-26T21:17:37","date_gmt":"2017-12-26T15:47:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26916"},"modified":"2017-12-26T21:17:37","modified_gmt":"2017-12-26T15:47:37","slug":"c-algorithm-search-element-linked-list-iterative-recursive","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-search-element-linked-list-iterative-recursive\/","title":{"rendered":"C Algorithm &#8211; Search an element in a Linked List both Iterative and Recursive"},"content":{"rendered":"<p>Write a C function that searches a given key \u2018x\u2019 in a given singly linked list. The function should return true if x is present in linked list and false otherwise.<span id=\"more-142794\"><\/span><\/p>\n<pre>   bool search(Node *head, int x)<\/pre>\n<p>For example, if the key to be searched is 15 and linked list is 14-&gt;21-&gt;11-&gt;30-&gt;10, then function should return false. If key to be searched is 14, then the function should return true.<\/p>\n<p><strong>Iterative Solution<\/strong><\/p>\n<pre>2) Initialize a node pointer, current = head.\r\n3) Do following while current is not NULL\r\n    a) current-&gt;key is equal to the key being searched return true.\r\n    b) current = current-&gt;next\r\n4) Return false<\/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\">\/\/ Iterative C program to search an element in 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 key;<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_key)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>            (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the key  *\/<br\/>    new_node-&gt;key  = new_key;<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\/>\/* Checks whether the value x is present in linked list *\/<br\/>bool search(struct node* head, int x)<br\/>{<br\/>    struct node* current = head;  \/\/ Initialize current<br\/>    while (current != NULL)<br\/>    {<br\/>        if (current-&gt;key == x)<br\/>            return true;<br\/>        current = current-&gt;next;<br\/>    }<br\/>    return false;<br\/>}<br\/> <br\/>\/* Driver program to test count function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/>    int x = 21;<br\/> <br\/>    \/* Use push() to construct below list<br\/>     14-&gt;21-&gt;11-&gt;30-&gt;10  *\/<br\/>    push(&amp;head, 10);<br\/>    push(&amp;head, 30);<br\/>    push(&amp;head, 11);<br\/>    push(&amp;head, 21);<br\/>    push(&amp;head, 14);<br\/> <br\/>    search(head, 21)? printf(&quot;Yes&quot;) : printf(&quot;No&quot;);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>Yes<\/pre>\n<p><strong>Recursive Solution<\/strong><\/p>\n<pre><strong>bool search(head, x)<\/strong>\r\n1) If head is NULL, return false.\r\n2) If head's key is same as x, return true;\r\n2) Else return search(head-&gt;next, x)<\/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\">\/\/ Recursive C program to search an element in 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 key;<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_key)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>            (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the key  *\/<br\/>    new_node-&gt;key  = new_key;<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\/>\/* Checks whether the value x is present in linked list *\/<br\/>bool search(struct node* head, int x)<br\/>{<br\/>    \/\/ Base case<br\/>    if (head == NULL)<br\/>        return false;<br\/>     <br\/>    \/\/ If key is present in current node, return true<br\/>    if (head-&gt;key == x)<br\/>        return true;<br\/> <br\/>    \/\/ Recur for remaining list<br\/>    return search(head-&gt;next, x);<br\/>}<br\/> <br\/>\/* Driver program to test count function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/>    int x = 21;<br\/> <br\/>    \/* Use push() to construct below list<br\/>     14-&gt;21-&gt;11-&gt;30-&gt;10  *\/<br\/>    push(&amp;head, 10);<br\/>    push(&amp;head, 30);<br\/>    push(&amp;head, 11);<br\/>    push(&amp;head, 21);<br\/>    push(&amp;head, 14);<br\/> <br\/>    search(head, 21)? printf(&quot;Yes&quot;) : printf(&quot;No&quot;);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>Yes<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Search an element in a Linked List both Iterative and Recursive &#8211;  Linked List &#8211; Write a C function that searches a given key \u2018x\u2019 in a given<\/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":[80299,80286,80289,80291,80290,80292,80298,80296,80294,80300,80293,80295,80297,80288,80287,80285],"class_list":["post-26916","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-linked-list","category-singly-linked-list","tag-algorithm-to-reverse-a-linked-list-in-data-structure","tag-algorithm-to-search-an-element-in-singly-linked-list","tag-binary-search-using-linked-list","tag-find-the-length-of-the-linked-list-using-recursion","tag-linked-list-recursion-c","tag-linked-list-recursion-java","tag-reverse-a-linked-list-algorithm","tag-reverse-a-linked-list-c","tag-reverse-a-linked-list-java","tag-reverse-a-linked-list-recursively-c","tag-reverse-a-linked-list-using-recursion-in-c","tag-reverse-a-singly-linked-list","tag-reverse-linked-list-recursive-java","tag-search-an-element-in-doubly-linked-list","tag-search-linked-list-complexity","tag-searching-in-linked-list-algorithm"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26916","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=26916"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26916\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}