Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Searching Linked List... help?? Posted by JT87 on 21 Nov 2007 at 3:16 PM
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:\n" + 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;
}
Report
Re: Searching Linked List... help?? Posted by HackmanC on 21 Nov 2007 at 7:08 PM
...to create code to search a linked list of linearnodes holding comparable items..
You should use a LinkedList Implementation with Comparable Interface Objects.

You are thinking on C (Pointers) Linked List.
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);
}

In Java LinkedList is implemented with Collections (The List Interface)
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());
}

Further information (Trail: Collections):
http://java.sun.com/docs/books/tutorial/collections/index.html


Good luck!
Hackman
Report
Re: Searching Linked List... help?? Posted by zibadian on 21 Nov 2007 at 7:57 PM
: 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:\n" + 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;
: }
:
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.
Report
Re: Searching Linked List... help?? Posted by JT87 on 25 Nov 2007 at 11:55 AM
: : 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:\n" + 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;
: : }
: :
: 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

Report
Re: Searching Linked List... help?? Posted by zibadian on 25 Nov 2007 at 12:27 PM
: : : 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;
: : : }
: : :
: : 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:
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  }

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.
Report
Re: Searching Linked List... help?? Posted by JT87 on 25 Nov 2007 at 12:40 PM
: : : : 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;
: : : : }
: : : :
: : : 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:
:
: 
: 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  }
: 
:
: 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





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.