0

Hey i have a program using while for looping, but i'm really confuse why it's become infinite loop

This is my code

print void

public void print() {
    DoublyLinkedListNode current = first;
    while (current != null) {
        current.displayInfo();
        current = current.next;
    }//end while
}//end print


public DoublyLinkedListNode partition(DoublyLinkedList list,
        DoublyLinkedListNode first, DoublyLinkedListNode last) {
    DoublyLinkedListNode smallIndex = first;
    DoublyLinkedListNode index = smallIndex.next;
    DoublyLinkedListNode temp = new DoublyLinkedListNode();
    double pivot = first.ipk;
    while (index != temp.next) {
        if ((index.ipk) < pivot) {
            smallIndex = smallIndex.next;
            temp.ipk = index.ipk;
            index.ipk = smallIndex.ipk;
            smallIndex.ipk = temp.ipk;
        }
        index = index.next;
    }
    temp.ipk = first.ipk;
    first.ipk = smallIndex.ipk;
    smallIndex.ipk = temp.ipk;
    System.out.println("The list in partition is: ");
    list.print();
    System.out.print("\n");
    return first;
}

public void recQuickSort(DoublyLinkedList list, DoublyLinkedListNode first,
        DoublyLinkedListNode last) {
    while (first != last) {
        DoublyLinkedListNode pivotLocation = partition(list, first, last);
        recQuickSort(list, first, pivotLocation.back);
        recQuickSort(list, pivotLocation.next, last);
    }
}

main

public static void main(String[] args) {
    DoublyLinkedList d = new DoublyLinkedList();
    d.insertNode("Apep", "123", 3.5);
    d.insertNode("Alex", "121", 3.2);
    d.insertNode("Kujul", "124", 3.1);
    d.insertNode("Fahmi", "125", 3.7);
    d.print();
    d.quickSort(d);
    d.print();
}

so from those code there will be infinite loop output and i don't know which is from my program that make it infinite loop.Thanks.

1

2 Answers 2

6

Well, just at a quick glance, you've got a a loop on while (first != last) and then you don't re-assign either one of those variables. Remember that != checks for reference equality ("are these the same exact object") not logical equality (as per the Object.equals(Object) method). So if first != last coming into that loop, you'll never exit that loop.

A similar thing happens previously, in the while (index != temp.next) loop.

If you've got a debugger, I would step through and try to figure out for yourself where the infinite loop is. If you haven't yet learned how to use a debugger, now would be a great time to learn. And if you don't have time for it yet, you can fall back to the time-tested trick of printing out debug lines (System.out.println("here!")) in various places. I'm sure one of them will start saying "here! here! here! here!" and tell you exactly where you're hitting the infinite loop.

Sign up to request clarification or add additional context in comments.

2 Comments

It's a recursive algorithm and first!=last looks like the base case - perhaps it should be if(first!=last)?
Good call, @selig. I think first == last is actually the base case, but yeah. What's happening is that this recurses down to the base case, at which point it returns, tries that same base case again, returns, tries that same base case again, ...
0

I see your link list is a doubly linked list. And somehow, any of your internal node's next holds the reference to your any other previously visited internal node.

If its like that, only then your while can go to infinite loop and the problem is in other part of the code.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.