target == p.getValue()
Here is the program, again:
import java.util.* ;
public class LinearSearch
{
public static void main ( String[] args )
{
// Build a linked list
Node node0 = new Node( 2 );
Node node1 = new Node( 3 ); node0.setNext( node1 );
Node node2 = new Node( 5 ); node1.setNext( node2 );
// Ask the user for a target
Scanner scan = new Scanner( System.in );
System.out.println("Number to search for: ");
int target = scan.nextInt();
// Traverse the Linked List in a loop.
// Stop when the target is found.
Node p = node0;
boolean found = false;
while ( p != null && !found )
{
if ( target == p.getValue() )
found = true;
else
p = p.getNext();
}
// Display the results
if ( found )
System.out.println( target + " is in the list." );
else
System.out.println( target + " is NOT in the list." );
}
}
There are many things to get correct. The traversal must
Does the program work correctly if several nodes hold the target?