this will be a little difficult to illustrate since the code is separated between several classes but I can't figure it out myself.
This code is supposed to first print "This list is empty." and then print each word within stringData in a single line:
public class Main
{
public static void main(String[] args)
{
LinkedList_1 list1 = new LinkedList_1(null);
list1.printList(list1.getRoot());
String stringData = "Harrier Ken Klaasje Titus Evrart Manaaba Cindy Eyckhead";
String[] data = stringData.split(" ");
for (int i = 0; i < (data.length); i++) {
list1.addItem(new Node(data[i]));
}
list1.printList(list1.getRoot());
}
}
The code prints "This list is empty." once and then seemingly never reaches the second print out line but there is no exception, notification, anything. I suspect that the problem hides in list1.addItem, which looks like this(the comments were only meant for myself):
@Override
public boolean addItem(ListItem newItem)
{
if (this.root == null) { // If no other item on list, newItem = first item on list
this.root = newItem;
return true;
}
ListItem current = this.root; // current = first item on list
while (current != null) { // As long as current exists, do
int comparison = (current.CompareTo(newItem)); // Alphabetically compares newItem and current. >0 = newItem comes AFTER current. <0 = newItem comes BEFORE current. 0 = newItem and current are identical.
// ........................................................................................................
if (comparison < 0) { // newItem should be inserted AFTER current
if (current.getNext() != null) { // If the list contains another item after current, then
current = current.getNext(); // current becomes next item in list.
// outcome AFTER-NOT LAST YET is completed and the while loop will repeat until newItem is either last or no longer comes after
} else { // if current is only item on list, then
current.setNextItem(newItem); // newItem comes after current
newItem.setPreviousItem(current); // current comes before after
return true; // outcome AFTER-LAST completed ---> drop out of the method/loop
} // ........................................................................................................
} else if (comparison > 0) { // newItem comes BEFORE current
if (current.getPrevious() != null) { // if current is not first in list, then
current.getPrevious().setNextItem(newItem); // newItem comes after item that comes before new item (1. previous 2. newItem)
newItem.setPreviousItem(current.getPrevious()); // previous comes before newItem
newItem.setNextItem(current); // current comes after newItem (1.previous 2. newItem 3. current)
current.setPreviousItem(newItem); // newItem comes before current
// outcome BEFORE-NOT FIRST YET is completed and the while loop will repeat until newItem is either first or no longer comes before
} else { // if current is first in list
newItem.setNextItem(this.root); // newItem comes before root (=current)
this.root.setPreviousItem(newItem); // root (=current) comes after newItem
this.root = newItem; // newItem is now root
return true; // outcome BEFORE-FIRST completed ---> drop out of the method/loop
}// ........................................................................................................
} else { // curent == newItem. As a duplicate, newItem is not added to the list
System.out.println(newItem.getValue() + " is already part of the List.class Duplicate not added.");
return false; // drop out of the method/loop without adding newItem to the list
} }
return false; // drop out of the loop without adding newItem to the list (how you would ever get here I don't know
}
Now maybe someone can already spot a problem or maybe its located in one of the other classes. Since I don't want to paste the whole code (although it's a rather short training program) into a new thread, maybe you could tell me which information is missing to understand the issue and I add or remove specific pieces?
In any case, thanks in advance for your help.
On second thought, let me add what .printList does right away.
public void printList(ListItem root)
{
if (root == null) {
System.out.println("This list is empty.");
} else {
while (root != null) {
System.out.println(root.getValue());
root = root.getNext();
}
}
}
ListItem class:
public abstract class ListItem
{
protected ListItem previousItem = null;
protected ListItem nextItem = null;
protected Object value;
public ListItem(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
abstract ListItem getNext();
abstract ListItem getPrevious();
abstract void setNextItem(ListItem n);
abstract void setPreviousItem(ListItem p);
abstract int CompareTo(ListItem item);
}
Node class:
public class Node extends ListItem
{
public Node(Object value) {
super(value);
}
@Override
ListItem getNext() {
return this.nextItem;
}
@Override
ListItem getPrevious() {
return this.previousItem;
}
@Override
void setNextItem(ListItem n) {
this.nextItem = n;
}
@Override
void setPreviousItem(ListItem p) {
this.previousItem = p;
}
@Override
public Object getValue() {
return value;
}
@Override
int CompareTo(ListItem item)
{
if (item != null) {
return ((String)super.getValue()).compareTo((String)item.getValue());
} else {
return -1;
}
}
}
You forgot to return in one case which is why it is going in infinite loop.
if (comparison > 0) { // newItem comes BEFORE current
if (current.getPrevious() != null) {
current.getPrevious().setNextItem(newItem);
newItem.setPreviousItem(current.getPrevious());
newItem.setNextItem(current);
current.setPreviousItem(newItem);
// return when element is added successfully