Orcka
Orcka

Reputation: 311

Infinite Loop on Linked List Priority Queue

After I insert a 3rd value into the queue, I get an infinite loop.

Code for insert Method:

    public boolean insert(E object) {
    Node<E> Nody = new Node(object);
    Node<E> current = head;
    Node<E> previous = null;
    if (isEmpty())
        head = Nody;
    while (current != null) {
        if (((Comparable<E>) object).compareTo(current.data) >= 0) {
            previous = current;
            current = current.next;
        } else {
            if (((Comparable<E>) object).compareTo(current.data) < 0) {
                previous = Nody;
                Nody = current;
            }
            if (previous == null && ((Comparable<E>) object).compareTo(current.data) < 0) {
                Nody.next = head;
                head = Nody;
            }
        }
    }
    currentSize++;
    return false;
}

Here is the driver I used and the place where I get the infinite loop:

    public P2Driver() {
    list = new OrderedListPriorityQueue<TestInteger>();
    randomInteger = new Random(8);
    randomPriority = new Random(8);

    print("");
    print("/////////////////////////////////////////////////////////");
    print("/// BEGIN TESTING LIST IMPLEMENTATION");
    print("/////////////////////////////////////////////////////////");
    print("");

    // Test insert
    printCurrentSize();
    print("--- BEGIN TESTING INSERT() --- ");
    print("Checking if the list is empty BEFORE insertion...");
    start = System.currentTimeMillis();
    for (int i = 0; i < MAX_SIZE; i++) {
        print(i + ". Is it empty? [ " + list.isEmpty() + " ]");
        list.insert(new TestInteger(randomInteger.nextInt(10)));
    } //GETS STUCK HERE<<<<<<<<<<<<<<<<<

Output:

 /////////////////////////////////////////////////////////
 /// BEGIN TESTING LIST IMPLEMENTATION
 /////////////////////////////////////////////////////////

 Current Size: 0
 --- BEGIN TESTING INSERT() --- 
 Checking if the list is empty BEFORE insertion...
 0. Is it empty? [ true ]
 1. Is it empty? [ false ]
 2. Is it empty? [ false ]

Since there is no exist code, this means the driver is stuck. I'm not sure what I am doing wrong.

Upvotes: 2

Views: 555

Answers (1)

Ogen
Ogen

Reputation: 6709

The else statement inside your while loop is not redefining current which is used in the conditional of your while loop so it's getting stuck there.

Upvotes: 1

Related Questions