Searching Linked List... help??

Working on my coursework here thats asks to create code to search a linked list of linearnodes holding comparable items. I've created the list but can't seem to get it so that when I enter a target value it comes up that it's not found in the list.. when its actually in the list. Hoping someone can have a look and help me out thanks.

import java.util.Scanner;
import java.io.*;
public class Wk6
{
public static int size =4;

public static void main(String[] args)throws IOException
{
Account1 user;
String name;
long account;
double initial;
Object target;
long answer;
int result;

LinearNode list = null;
LinearNode temp;

Scanner sc = new Scanner(new File("hello.txt"));
Scanner kbScan = new Scanner(System.in);


for(int i =0; i<size; i++)
{

name = sc.next();
account = sc.nextLong();
initial = sc.nextDouble();

user = new Account1(name, account, initial);
temp = new LinearNode(user);
temp.setNext(list);
list = temp;
}

printList(list);
countList(list);

System.out.println("Enter an account number you would like to search for:");
answer = kbScan.nextLong();
target=answer;

result=search(list, target);

if(result!=-1){
System.out.println("Found in position:
" + result);
}
else {
System.out.println("Not found");
}

}

public static void printList(LinearNode L)
{
LinearNode temp = L;

while (temp != null)
{
System.out.println(temp.getElement());
temp = temp.getNext();
}

}

// iterative Method
public static void countList(LinearNode L)
{

LinearNode temp = L;
int counter = 0;

while(temp != null)
{
counter++;
temp = temp.getNext();
}

System.out.println("Number of Counters:" + counter);
}


public static int search(LinearNode L, Object target)
{
LinearNode temp = L;

int index =0;
int found = -1;
while(found==-1 && index<size)
{


if ((temp.getElement()).equals(target))

found = index;
index++;
L=(L.getNext()).getNext();


}
return found;
}[color=Blue][/color]

Comments

  • [italic]...to create code to search a linked list of linearnodes holding comparable items..[/italic]
    You should use a LinkedList Implementation with Comparable Interface Objects.

    You are thinking on C (Pointers) Linked List.
    [code]
    temp.setNext(list);
    list = temp;

    while (temp != null)
    {
    System.out.println(temp.getElement());
    temp = temp.getNext();
    }

    public static void countList(LinearNode L) {
    LinearNode temp = L;
    int counter = 0;
    while(temp != null)
    {
    counter++;
    temp = temp.getNext();
    }
    System.out.println("Number of Counters:" + counter);
    }
    [/code]
    In Java LinkedList is implemented with Collections (The List Interface)
    [code]
    LinkedList.add(item);
    List.add(item);

    for (Account1 account : allAccounts) {
    System.out.println(account.name);
    }

    public static void countList(LinkedList L) {
    System.out.println("Number of Accounts:" + L.size().toString());
    }
    [/code]
    Further information (Trail: Collections):
    http://java.sun.com/docs/books/tutorial/collections/index.html


    [red]Good luck![/red]
    [blue]Hackman[/blue]
  • : Working on my coursework here thats asks to create code to search a
    : linked list of linearnodes holding comparable items. I've created
    : the list but can't seem to get it so that when I enter a target
    : value it comes up that it's not found in the list.. when its
    : actually in the list. Hoping someone can have a look and help me out
    : thanks.
    :
    : import java.util.Scanner;
    : import java.io.*;
    : public class Wk6
    : {
    : public static int size =4;
    :
    : public static void main(String[] args)throws IOException
    : {
    : Account1 user;
    : String name;
    : long account;
    : double initial;
    : Object target;
    : long answer;
    : int result;
    :
    : LinearNode list = null;
    : LinearNode temp;
    :
    : Scanner sc = new Scanner(new File("hello.txt"));
    : Scanner kbScan = new Scanner(System.in);
    :
    :
    : for(int i =0; i<size; i++)
    : {
    :
    : name = sc.next();
    : account = sc.nextLong();
    : initial = sc.nextDouble();
    :
    : user = new Account1(name, account, initial);
    : temp = new LinearNode(user);
    : temp.setNext(list);
    : list = temp;
    : }
    :
    : printList(list);
    : countList(list);
    :
    : System.out.println("Enter an account number you would like
    : to search for:");
    : answer = kbScan.nextLong();
    : target=answer;
    :
    : result=search(list, target);
    :
    : if(result!=-1){
    : System.out.println("Found in position:
    " + result);
    : }
    : else {
    : System.out.println("Not found");
    : }
    :
    : }
    :
    : public static void printList(LinearNode L)
    : {
    : LinearNode temp = L;
    :
    : while (temp != null)
    : {
    : System.out.println(temp.getElement());
    : temp = temp.getNext();
    : }
    :
    : }
    :
    : // iterative Method
    : public static void countList(LinearNode L)
    : {
    :
    : LinearNode temp = L;
    : int counter = 0;
    :
    : while(temp != null)
    : {
    : counter++;
    : temp = temp.getNext();
    : }
    :
    : System.out.println("Number of Counters:" + counter);
    : }
    :
    :
    : public static int search(LinearNode L, Object target)
    : {
    : LinearNode temp = L;
    :
    : int index =0;
    : int found = -1;
    : while(found==-1 && index<size)
    : {
    :
    :
    : [red]if ((temp.getElement()).equals(target))[/red]
    :
    : found = index;
    : index++;
    : [red]L=(L.getNext()).getNext();[/red]
    :
    :
    : }
    : return found;
    : }[color=Blue][/color]
    :
    In your search() method you skip every other node, and you compare the target with only the first node. I've marked the wrong lines in red above.
  • : : Working on my coursework here thats asks to create code to search a
    : : linked list of linearnodes holding comparable items. I've created
    : : the list but can't seem to get it so that when I enter a target
    : : value it comes up that it's not found in the list.. when its
    : : actually in the list. Hoping someone can have a look and help me out
    : : thanks.
    : :
    : : import java.util.Scanner;
    : : import java.io.*;
    : : public class Wk6
    : : {
    : : public static int size =4;
    : :
    : : public static void main(String[] args)throws IOException
    : : {
    : : Account1 user;
    : : String name;
    : : long account;
    : : double initial;
    : : Object target;
    : : long answer;
    : : int result;
    : :
    : : LinearNode list = null;
    : : LinearNode temp;
    : :
    : : Scanner sc = new Scanner(new File("hello.txt"));
    : : Scanner kbScan = new Scanner(System.in);
    : :
    : :
    : : for(int i =0; i<size; i++)
    : : {
    : :
    : : name = sc.next();
    : : account = sc.nextLong();
    : : initial = sc.nextDouble();
    : :
    : : user = new Account1(name, account, initial);
    : : temp = new LinearNode(user);
    : : temp.setNext(list);
    : : list = temp;
    : : }
    : :
    : : printList(list);
    : : countList(list);
    : :
    : : System.out.println("Enter an account number you would like
    : : to search for:");
    : : answer = kbScan.nextLong();
    : : target=answer;
    : :
    : : result=search(list, target);
    : :
    : : if(result!=-1){
    : : System.out.println("Found in position:
    " + result);
    : : }
    : : else {
    : : System.out.println("Not found");
    : : }
    : :
    : : }
    : :
    : : public static void printList(LinearNode L)
    : : {
    : : LinearNode temp = L;
    : :
    : : while (temp != null)
    : : {
    : : System.out.println(temp.getElement());
    : : temp = temp.getNext();
    : : }
    : :
    : : }
    : :
    : : // iterative Method
    : : public static void countList(LinearNode L)
    : : {
    : :
    : : LinearNode temp = L;
    : : int counter = 0;
    : :
    : : while(temp != null)
    : : {
    : : counter++;
    : : temp = temp.getNext();
    : : }
    : :
    : : System.out.println("Number of Counters:" + counter);
    : : }
    : :
    : :
    : : public static int search(LinearNode L, Object target)
    : : {
    : : LinearNode temp = L;
    : :
    : : int index =0;
    : : int found = -1;
    : : while(found==-1 && index<size)
    : : {
    : :
    : :
    : : [red]if ((temp.getElement()).equals(target))[/red]
    : :
    : : found = index;
    : : index++;
    : : [red]L=(L.getNext()).getNext();[/red]
    : :
    : :
    : : }
    : : return found;
    : : }[color=Blue][/color]
    : :
    : In your search() method you skip every other node, and you compare
    : the target with only the first node. I've marked the wrong lines in
    : red above.


    Thanks I knew it was something to do with the search method because everything else seems to work ok. Just can't get that bit right, any ideas on how I can get it to search every other one instead of just the first one?? Thanks x

  • : : : public static int search(LinearNode L, Object target)
    : : : {
    : : : LinearNode temp = L;
    : : :
    : : : int index =0;
    : : : int found = -1;
    : : : while(found==-1 && index<size)
    : : : {
    : : :
    : : :
    : : : [red]if ((temp.getElement()).equals(target))[/red]
    : : :
    : : : found = index;
    : : : index++;
    : : : [red]L=(L.getNext()).getNext();[/red]
    : : :
    : : :
    : : : }
    : : : return found;
    : : : }[color=Blue][/color]
    : : :
    : : In your search() method you skip every other node, and you compare
    : : the target with only the first node. I've marked the wrong lines in
    : : red above.
    :
    :
    : Thanks I knew it was something to do with the search method because
    : everything else seems to work ok. Just can't get that bit right, any
    : ideas on how I can get it to search every other one instead of just
    : the first one?? Thanks x
    :
    :
    First you need to check which variable holds which value:

    - L is the first node
    - temp is the currect node

    In your code you are checking if temp equals the target. Then you move the L variable to the next's next node. Here's a simple example of how your code performs its search:
    [code]
    1 LinearNode temp = L;
    2 while(found==-1 && index<size) {
    3 if ((temp.getElement()).equals(target))
    4 found = index;
    5 L=(L.getNext()).getNext();
    6 }
    [/code]
    given this list: 1, 2, 3, 4, 5 and target = 2.
    Before line 1: L = 1
    Then after line 1: temp = 1
    In line 3 you compare temp to the target (i.e. 1.equals(2)) which is false.
    In line 5 you call L.getNext() (L = 2, inside brackets) and immediately call it again (L = 3, outside the brackets).
    Then back to line 2, and the loop continues.
    Again line 3 is executed: temp is compared to the target (i.e. 1.equals(2)) which is false.
    And then line 5 is executed again: L = 4 and L = 5.
    For the last time back to line 3.
    Still comparing temp to the target.
    Now inside the brackets in line 5 the next node is retrieved (L = null). And then getNext() is called again, but this time the object is null, throwing a NullPointerException.
    As you can see there are 2 mistakes in your code: line 3 and line 5. In line 3 you're checking the value of temp, but that variable always remains at the first node. In line 5 you're updating the variable L twice per iteration through the loop. This should only happen once.
  • : : : : public static int search(LinearNode L, Object target)
    : : : : {
    : : : : LinearNode temp = L;
    : : : :
    : : : : int index =0;
    : : : : int found = -1;
    : : : : while(found==-1 && index<size)
    : : : : {
    : : : :
    : : : :
    : : : : [red]if ((temp.getElement()).equals(target))[/red]
    : : : :
    : : : : found = index;
    : : : : index++;
    : : : : [red]L=(L.getNext()).getNext();[/red]
    : : : :
    : : : :
    : : : : }
    : : : : return found;
    : : : : }[color=Blue][/color]
    : : : :
    : : : In your search() method you skip every other node, and you compare
    : : : the target with only the first node. I've marked the wrong lines in
    : : : red above.
    : :
    : :
    : : Thanks I knew it was something to do with the search method because
    : : everything else seems to work ok. Just can't get that bit right, any
    : : ideas on how I can get it to search every other one instead of just
    : : the first one?? Thanks x
    : :
    : :
    : First you need to check which variable holds which value:
    :
    : - L is the first node
    : - temp is the currect node
    :
    : In your code you are checking if temp equals the target. Then you
    : move the L variable to the next's next node. Here's a simple example
    : of how your code performs its search:
    : [code]:
    : 1 LinearNode temp = L;
    : 2 while(found==-1 && index<size) {
    : 3 if ((temp.getElement()).equals(target))
    : 4 found = index;
    : 5 L=(L.getNext()).getNext();
    : 6 }
    : [/code]:
    : given this list: 1, 2, 3, 4, 5 and target = 2.
    : Before line 1: L = 1
    : Then after line 1: temp = 1
    : In line 3 you compare temp to the target (i.e. 1.equals(2)) which is
    : false.
    : In line 5 you call L.getNext() (L = 2, inside brackets) and
    : immediately call it again (L = 3, outside the brackets).
    : Then back to line 2, and the loop continues.
    : Again line 3 is executed: temp is compared to the target (i.e.
    : 1.equals(2)) which is false.
    : And then line 5 is executed again: L = 4 and L = 5.
    : For the last time back to line 3.
    : Still comparing temp to the target.
    : Now inside the brackets in line 5 the next node is retrieved (L =
    : null). And then getNext() is called again, but this time the object
    : is null, throwing a NullPointerException.
    : As you can see there are 2 mistakes in your code: line 3 and line 5.
    : In line 3 you're checking the value of temp, but that variable
    : always remains at the first node. In line 5 you're updating the
    : variable L twice per iteration through the loop. This should only
    : happen once.

    Thanks, I understand what you mean. Going to have a go at it now, thanks x


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories