1

I've been messing with this for a while now and it seems like no matter what I do my output is always truncated or "sorted" arbitrarily. Is moving the references around enough to swap the two elements? I'm trying to make it so that, if the current node's SSN field is larger than the previous one, link the previous one to the current one. This is how my code looks at the moment:

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;
    }
}

1 Answer 1

0

You likely need something like this

                Node tmp = head.nextOne
                head.nextOne = prev.nextOne;
                prev.nextOne = tmp;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.