一般的な順序付きリンク リスト クラスがあります。何らかの理由で、add() を実行するたびに LinearNode ヘッドが再割り当てされます。何か案は?これを実行するたびにヘッドが変更されるのはなぜですか? 私もそれに触れていません。必要に応じて、テスト用に他のクラスを提供できます。
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++;
}
ご協力いただきありがとうございます。