Hello so i was unable to complete my assignment because I had difficulties figuring out how to set up the search method within my linked list.. Now I still want to learn how to configure the linked list and i cant make any of the other methods such as remove, or replace if i cant figure out the search method. for some reason my results keep turning out null This is what i have so far..
public class WordList {
private WordMeaningNode list;
WordList()
{
list = null;
}
void add(WordMeaning b)
{
WordMeaningNode temp = new WordMeaningNode(b);
try
{
temp.Next = list;
list = temp;
}
catch(NullPointerException e)
{
System.out.println("Null Pointer Exception");
}
}
public String toString()
{
String result="";
WordMeaningNode current = list;
while (current != null)
{
result += current.Words.word + " - " + current.Words.meaning + "\n";
current = current.Next;
}
return result;
}
public WordMeaning findword (WordMeaning Word)
{
WordMeaningNode Checker = new WordMeaningNode(Word);
boolean found = false;
if (list.isEmpty() == true)
{
return null;
}
else
{
while(list.Next != null && !found)
{
if(list.Words.word.compareTo(Checker.Words.word)== 0)
{
found = true;
}
else
{
list = list.Next;
found = false;
}
}
}
if(found == true)
{
return list.Words;
}
else
{
return null;
}
}
2 Answers 2
You version actually changes the list
(which is actually the root (or first) node of your list) itself in every iteration which breaks the list. Your query method should not alter the list itself. Simply use a temporary (local) node variable (mine is called current
) in your while loop instead, like so:
public WordMeaning findWord (WordMeaning word)
{
if (list.isEmpty())
{
return null;
}
boolean found = false;
WordMeaningNode current = list;
while(current != null)
{
if(current.Words.word.compareTo(word)== 0)
{
found = true;
break;
}
current = current.Next;
}
return current;
}
-
Okay so the issue is my while loop not the if statements outside of the loop correct?... alright now is it because of the "&& !found" part which is messing up my loop because i think that part would do the same roles as the "break" you have in your codeXavier– Xavier11/30/2013 09:29:31Commented Nov 30, 2013 at 9:29
-
You are right, the break eliminates the need of checking against found. This thread discusses the merits of using break and continue.Domi– Domi11/30/2013 09:31:03Commented Nov 30, 2013 at 9:31
-
You can't return current because current is a WordMeaningNode.. but i get what your saying my confusion lay in the fact that my code does the same thing a bit longer though.Xavier– Xavier11/30/2013 09:32:03Commented Nov 30, 2013 at 9:32
-
I apologize. I changed your code to also clean up some things. The main difference is the
WordMeaningNode current
over which we iterate instead of using and modifying the list root calledlist
as you are doing.Domi– Domi11/30/2013 09:33:34Commented Nov 30, 2013 at 9:33 -
Okay.. but even changing that wont stop the null pointer.. im fairly new with debugging in netbeans but it seams to run through my loop and then reach the word, which should end the loop and return found as true, but it skips it and goes straight to nullXavier– Xavier11/30/2013 09:38:05Commented Nov 30, 2013 at 9:38
This is an example of how to find an element in a linked list:
public class LinkedList<T> {
private LinkedList<T> next;
private T data;
public LinkedList(T value) {
data = value;
}
public LinkedList<T> next() {
return next;
}
public T value() {
return data;
}
public void setNext(LinkedList<T> element) {
next = element;
}
public void setValue(T value) {
data = value;
}
}
public LinkedList<Integer> find(LinkedList<Integer> head, int data) {
LinkedList<Integer> elem = head;
while (elem != null && elem.value() != data) {
elem = elem.next();
}
return elem;
}
list
inside your findword method. ou shouldn't do that. You should also read the Java naming conventions and stick to them.list.Next != null
tolist != null