Search an element in a linked list

Program to Search an Element in a linked list

Program to Search an element in a linked list is performed in order to find the location of a particular element in the list. Searching any element in the list needs traversing through the list and make the comparison of every element of the list with the specified element. If the element is matched with any of the list element then the location of the element is returned from the function.

Algorithm to search an element in a Linked List.

Step 1– start

Step 2– initialize a class

step 3– int data; node next; node (int d)

Step 4– initialize a linked list class

Step 5– insert a new node // ( int new_node)

Step 6– node new_node = new Node (new_data)

Step 7- make next node as new node // new_node.next = head

Step 8– move head to new Node// head= new_node

Step 10– Check whether the value is present in linked list

Step 11- Initialize current node

Step 12– run a while loop to find data in current node

Step 13– Add a function to test the above functions

Step 14– input a empty linked list

Step 15– Input data in a empty list

Step 16– Print (“yes”) if the searching number is in the given list

Step 17– else // print (“No”)

Step 18– End .

Search an Element in a Linked List

Java Program for Searching an element in a Linked List

Run
//Java program to search an element  in linked list 

import java.lang.*;

class LinkedList
{
  Node head;

  // Node Class
  class Node
  {
    int data;
    Node next;

      Node (int x)		// parameterized constructor
    {
      data = x;
      next = null;
    }
  }
  //searchNode() will search for a given node in the list
  public void searchNode (int data)
  {
    Node current = head;
    int i = 1;
    boolean flag = false;

    //Checks whether list is empty
    if (head == null)
      {
	System.out.println ("List is empty");
      }
    else
      {
	while (current != null)
	  {
	    //Compares node to be found with each node present in the list
	    if (current.data == data)
	      {
		flag = true;
		break;
	      }
	    i++;
	    current = current.next;
	  }
      }
    if (flag)
      System.out.
	println ("Element is present in the list at the position : " + i);
    else
      System.out.println ("Element is not present in the list");
  }

  public Node insert (int data)
  {
    Node newNode = new Node (data);
    newNode.next = head;
    head = newNode;

    return head;
  }

  public void display ()
  {
    Node node = head;
    //as linked list will end when Node reaches Null
    while (node != null)
      {
	System.out.print (node.data + " ");
	node = node.next;
      }
    System.out.println ("\n");
  }



}

public class Main
{
  public static void main (String args[])
  {
    LinkedList ll = new LinkedList ();

      ll.insert (6);
      ll.insert (5);
      ll.insert (4);
      ll.insert (3);
      ll.insert (2);
      ll.insert (1);

    //Search for node 2 in the list
      ll.searchNode (2);
    //Search for a node  in the list
      ll.searchNode (7);
  }
}
Element is present in the list at the position : 2
Element is not present in the list

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java