I have a generic ordered Linked List class. For some reason, LinearNode head is being reassigned every single time I run add(). Any ideas? Why would head be changed each time I run this? I’m not even touching it. I can provide the other classes for testing, if needed.
public class myOrLiList<T extends Comparable<T>> {
public LinearNode head;
public int count;
public myOrLiList() {
head = null;
count = 0;
}
// LinearNode INNER CLASS
public class LinearNode {
public LinearNode next;
public T item;
public LinearNode(T thisitem) {
this.next = null;
this.item = thisitem;
}
}
public boolean isEmpty() {
return (head == null);
}
public void add(T thisItem) {
LinearNode newNode = new LinearNode(thisItem);
if (isEmpty()) {
head = newNode;
System.out.println("head filled!");
} else {
LinearNode compareNode = head;
do {
if (thisItem.compareTo(compareNode.item) < 0) {
newNode.next = compareNode;
break;
} else if ((thisItem.compareTo(compareNode.item) < 0)
|| (thisItem.compareTo(compareNode.item) == 0)) {
newNode.next = compareNode.next;
compareNode.next = newNode;
break;
} else {
compareNode = compareNode.next;
}
} while (compareNode.next != null);
}
System.out.println("Added!");
count++;
}
Thanks for your help.
While this class has some errors (Thanks for the tips, Michael!) the question I asked did not come from these errors.
In the new object class I had created for T, I had declared static properties. So, each time a new T thisItem was being created, all of the properties of all objects were being changed.