{"id":26917,"date":"2017-12-26T21:15:23","date_gmt":"2017-12-26T15:45:23","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26917"},"modified":"2017-12-26T21:15:23","modified_gmt":"2017-12-26T15:45:23","slug":"java-algorithm-search-element-linked-list-iterative-recursive","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-search-element-linked-list-iterative-recursive\/","title":{"rendered":"Java 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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Iterative Java program to search an element<br\/>\/\/ in linked list<br\/> <br\/>\/\/Node class<br\/>class Node<br\/>{<br\/>    int data;<br\/>    Node next;<br\/>    Node(int d)<br\/>    {<br\/>        data = d;<br\/>        next = null;<br\/>    }<br\/>}<br\/> <br\/>\/\/Linked list class<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/Head of list<br\/> <br\/>    \/\/Inserts a new node at the front of the list<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/\/Allocate new node and putting data<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/\/Make next of new node as head<br\/>        new_node.next = head;<br\/> <br\/>        \/\/Move the head to point to new Node<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/\/Checks whether the value x is present in linked list<br\/>    public boolean search(Node head, int x)<br\/>    {<br\/>        Node current = head;    \/\/Initialize current<br\/>        while (current != null)<br\/>        {<br\/>            if (current.data == x)<br\/>                return true;    \/\/data found<br\/>            current = current.next;<br\/>        }<br\/>        return false;   \/\/data not found<br\/>    }<br\/> <br\/>    \/\/Driver function to test the above functions<br\/>    public static void main(String args[])<br\/>    {<br\/> <br\/>        \/\/Start with the empty list<br\/>        LinkedList llist = new LinkedList();<br\/> <br\/>        \/*Use push() to construct below list<br\/>        14-&gt;21-&gt;11-&gt;30-&gt;10  *\/<br\/>        llist.push(10);<br\/>        llist.push(30);<br\/>        llist.push(11);<br\/>        llist.push(21);<br\/>        llist.push(14);<br\/> <br\/>        if (llist.search(llist.head, 21))<br\/>            System.out.println(&quot;Yes&quot;);<br\/>        else<br\/>            System.out.println(&quot;No&quot;);<br\/>    }<br\/>}<br\/>\/\/ This code is contributed by Pratik Agarwal<\/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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Recursive Java program to search an element<br\/>\/\/ in linked list<br\/> <br\/> <br\/>\/\/ Node class<br\/>class Node<br\/>{<br\/>    int data;<br\/>    Node next;<br\/>    Node(int d)<br\/>    {<br\/>        data = d;<br\/>        next = null;<br\/>    }<br\/>}<br\/> <br\/>\/\/ Linked list class<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/Head of list<br\/> <br\/>    \/\/Inserts a new node at the front of the list<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/\/Allocate new node and putting data<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/\/Make next of new node as head<br\/>        new_node.next = head;<br\/> <br\/>        \/\/Move the head to point to new Node<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/\/ Checks whether the value x is present<br\/>    \/\/ in linked list<br\/>    public boolean search(Node head, int x)<br\/>    {<br\/>        \/\/ Base case<br\/>        if (head == null)<br\/>            return false;<br\/> <br\/>        \/\/ If key is present in current node,<br\/>        \/\/ return true<br\/>        if (head.data == x)<br\/>            return true;<br\/> <br\/>        \/\/ Recur for remaining list<br\/>        return search(head.next, x);<br\/>    }<br\/> <br\/>    \/\/ Driver function to test the above functions<br\/>    public static void main(String args[])<br\/>    {<br\/>        \/\/ Start with the empty list<br\/>        LinkedList llist = new LinkedList();<br\/> <br\/>        \/* Use push() to construct below list<br\/>           14-&gt;21-&gt;11-&gt;30-&gt;10  *\/<br\/>        llist.push(10);<br\/>        llist.push(30);<br\/>        llist.push(11);<br\/>        llist.push(21);<br\/>        llist.push(14);<br\/> <br\/>        if (llist.search(llist.head, 21))<br\/>            System.out.println(&quot;Yes&quot;);<br\/>        else<br\/>            System.out.println(&quot;No&quot;);<br\/>    }<br\/>}<br\/>\/\/ This code is contributed by Pratik Agarwal<\/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>Java 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":[1,2139,79476,79478],"tags":[80299,80286,80289,80291,80290,80292,80298,80296,80294,80300,80293,80295,80297,80288,80287,80285],"class_list":["post-26917","post","type-post","status-publish","format-standard","hentry","category-coding","category-java","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\/26917","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=26917"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26917\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}