1

我已经搞砸了一段时间,似乎无论我做什么,我的输出总是被任意截断或“排序”。移动参考是否足以交换两个元素?我正在努力做到这一点,如果当前节点的 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;
    }
}
4

1 回答 1

0

你可能需要这样的东西

                Node tmp = head.nextOne
                head.nextOne = prev.nextOne;
                prev.nextOne = tmp;
于 2013-03-15T23:14:02.503 回答