Java Program to Search for an Element in a Singly Linked List

In this article we are going to see how we can search for an element in a singly linked list by using Java programming language.

Java Program to Search for an Element in a Singly Linked List

Approach:

  • Create a linked list.
  • Add elements to it.
  • Display the list.
  • Ask the user to enter an element to search.
  • Pass the element into our user defined method search( ) method. The method iterates the whole list, compares the value.
  • Then prints the result.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // method to search for a value in a linked list
    public void search(int data) 
    {  
        Node curr = head;  
        // iterator
        int i = 1;  
        // Flag to check the condition
        boolean flag = false;  
        if(head == null) 
        {  
            System.out.println("Linked list is empty");  
        }
        else 
        {  
            // Iterates the whole list
            while(curr != null)
            {  
                // compares the value with the data at each node
                if(curr.data == data) 
                {
                    // If a match is found breaks out of the loop  
                    flag = true;  
                    break;  
                }  
                i++;  
                curr = curr.nextNode;  
            }  
        }  
        if(flag)  
            System.out.println("Element is at location : " + i);  
        else  
            System.out.println("Element could not be found");  
    }  

    // method to add a node to the end of the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a value to search");
        int val = sc.nextInt();
        ll.search(val);
    }
}
Output:

The nodes are:
10,20,30,40,50,
Enter a value to search
50
Element is at location : 5

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.