How to implement Search Function using Singly Linked List in Java?
<p>Hello fellow Programmers!
I would like to ask how to implement the Search Function in Singly Linked List using Java (Data Structures).</p>

<p>PROBLEM: The Search function of my program can only access the head and tail of the elements.
Question: How can I access all the elements?</p>

<p>Sample Code:</p>

<pre><code>import java.util.*;
public class MyLinkedList&lt;T&gt; {
    private Node&lt;T&gt; head;
    private Node&lt;T&gt; tail;

public void add(T element){
    Node&lt;T&gt; nd = new Node&lt;T&gt;();
    nd.setValue(element);
     if(head == null){
        head = nd;
        tail = nd;
      }
     else {
        tail.setNextRef(nd);
        tail = nd;
      }}
 public void delete(){
    if(head == null){
        System.out.println("List is empty...");
    }
    else{
    Node&lt;T&gt; tmp = head;
    head = tmp.getNextRef();
    if(head == null){
        tail = null;
    }
    System.out.println("Deleted: "+tmp.getValue());
    System.out.println("First Employee Name Deleted!");
    }
}
public void search(){
    Node&lt;T&gt; tmp = head;
        if(head==null){
            System.out.println("List is empty...");
        }
        else{
        String b=""+tmp.getValue();
        Scanner input=new Scanner(System.in);
        System.out.print("Enter an employee name to search: ");
        String search = input.nextLine();
        if(b.equals(search)){
         System.out.println("Found! "+tmp.getValue());
        }
        else if (b!=search){
            System.out.println("Not Found!");
        }
        tmp = tmp.getNextRef();
    }   }
  public void show(){
    Node&lt;T&gt; tmp = head;
    if(head==null){
        System.out.println("List is empty...");
    }
    else{
        while(true){
        if(tmp == null){
            break;
        }

        System.out.println(tmp.getValue());
        tmp = tmp.getNextRef();
    }}
}
public static void main(String a[]){
    MyLinkedList&lt;String&gt; sl = new MyLinkedList&lt;String&gt;();
    Scanner input = new Scanner(System.in);
    sl.add("Garcia, Bianca Axel, 001");
    sl.add("Temprosa, Camille Ann, 002");
    sl.add("Villanueva, Von Justin, 003");
    sl.add("Rivera, Reygie, 004");
    sl.add("Dapapac, Ronnelle, 005");
    sl.add("Bati, Aubrey, 006");
    sl.add("Fiestada, Diana Rose, 007");
    sl.add("Dobalada, Jojo, 008");
    sl.add("Del Mundo, Maria Ethel, 009");
    sl.add("Alejandro, Rachelle, 010");
    System.out.print("Welcome To Singly Linked List Management for Employee Names!\n(Created by: Alex del Rosario || Source Code: Mr. John Carlo Son)\n");
    while(true){
    System.out.println("*------------------------*");
    System.out.println("*PLEASE SELECT A FUNCTION*");
    System.out.println("*1.)Add Employee Name    *");
    System.out.println("*2.)Delete Employee Name *");
    System.out.println("*3.)Show Employee List   *");
    System.out.println("*4.)Search an Employee   *");
    System.out.println("*5.)Exit the program     *");
    System.out.println("*------------------------*");
    System.out.print("Enter a number: ");
    int select = input.nextInt();
    if(select==1){
    System.out.println("**Enter an employee name with ID number**");
    System.out.println("Use this format:(Last Name),(First Name),(ID number)");
    System.out.println("(ex. del Rosario, Alex, 0001)");
    System.out.print("Input here: ");
    input.nextLine();
    String employee=input.nextLine();
    sl.add(employee);
    System.out.println("Employee: "+employee+" successfully added!");
    }
    else if(select==2){
    sl.delete();
    }
    else if(select==3){
    System.out.println("Employee List:");
    sl.show();
    }
    else if(select==4){
    sl.search();
    }
    else if(select==5){
    System.out.println("*** THANK YOU FOR USING THIS PROGRAM! :) ***");
    System.exit(0);
    }
    else{
        System.out.println("INVALID INPUT!");
    }

    }}
}

class Node &lt;T&gt; implements Comparable&lt;T&gt; {

    private T value;
    private Node&lt;T&gt; nextRef;

    public T getValue() {
        return value;
    }
    public void setValue(T value) {
        this.value = value;
    }
    public Node&lt;T&gt; getNextRef() {
        return nextRef;
    }
    public void setNextRef(Node&lt;T&gt; ref) {
        this.nextRef = ref;
    }
    @Override
    public int compareTo(T arg) {
        if(arg == this.value){
            return 0;
        } else {
            return 1;
        }
    }
}
</code></pre>