我现在已经搞砸了一段时间,似乎无论我做什么,我的输出总是被截断或“任意排序”。移动引用足以交换这两个元素?我试图这样做,如果当前节点的SSN字段大于前一个,将前一个链接到当前节点。这就是我的代码目前的样子:
public void loadRecords() throws FileNotFoundException {
Node head = null;
Node prev = null;
Node curr = null;
Scanner fileRead = makeAFile(database);
fileRead.useDelimiter(";|\n");
boolean sorted = false;
while (fileRead.hasNext()) {
head = new Node(fileRead.next(), fileRead.next(), fileRead.next());
head.nextOne = prev;
if (prev != null) {
for (curr = head; curr != null; curr = curr.nextOne) {
if ((curr).compareTo(prev) > 0) {
head.nextOne = prev.nextOne; //oops, there we go. used to be "head.nextOne = curr.nextOne".
prev = head; // not even sure if this is what I want
sorted = true;
} else
break;
}
}
if (sorted != true) {
prev = head;
}
sorted = false;
}
}
答案 0 :(得分:0)
你可能需要这样的东西
Node tmp = head.nextOne
head.nextOne = prev.nextOne;
prev.nextOne = tmp;