I'm running a rather straight-forward method, that adds words to a list in alphabetical order.
For some reason, whenever the "addToData" method reaches its end, it wont return to its original caller in the main method, but instead just stops all together.
I've debugged the code, and it doesn't enter any infinite loops, or gets stuck with exceptions.
Main Method:
// Create the first list, with empty constructor
TextList list0 = new TextList();
// Check the method - addToData
System.out.println(list0);
list0.addToData("hello");
System.out.println("list0 after adding the word hello: \n"+list0);
list0.addToData("hello");
System.out.println("list0 after adding the word hello twice: \n"+list0);
The method in which im getting stuck in:
public void addToData(String word){// O(n) -> Because we are going over the whole list only once (n times)
if("".equals(word))
return;
if(_head == null) {
_head = new WordNode(word);
return;
}
WordNode temp = _head;
int n = 0;
WordNode ptr = temp.getNext();
//pointer to the next word, and test on it so we'll always have a pointer to the previous word
while(temp.getNext() != null){
n = ptr.getWord().compareTo(word);
if(n <= 0){
temp.setNext(new WordNode(word));
temp.getNext().setNext(ptr);
return;
}
else {temp = ptr;
ptr = ptr.getNext();
}
}
temp.setNext(new WordNode(word));//if we went through the whole list, that means the word is the largest and comes last
}
Only thing printed to console:
list0 after adding the word hello: hello 1
Due to multiple questions, you may find the entire project code here.
I am using IntelliJ, in case it might have anything to do with the issue...
Any and all suggestions would be appreciated, as I cant seem to find anything regarding this issue.