1

So i am coding a doubly linked list implementation in java and i need to print out values so that I can test to see if the code works. However, for the past couple of days ive been trying to figure out why I keep getting an infinite loop. Here's my code:

public class DoublyLinkedList<T> implements LinkedListInterface<T>, Iterable<T>{

private Node<T> head;
private Node<T> tail;
private int size;

//add, remove, clear, isempty methods, etc.

private class LinkedListIterator<E> implements java.util.Iterator<E> {

    private Node<E> probe;

    public LinkedListIterator(Node<T> head) {
        probe = (Node<E>)(head);
    }
    public boolean hasNext() {
        return (probe!= null);
    }

    public E next() {
        if (!hasNext()) {
            throw new NoSuchElementException("There is no element.");
        }
        E temp = probe.getData();
        probe = probe.getNext();
        return temp;
    }

    public void remove() {
        throw new UnsupportedOperationException("We don't support this function.");
    }

}

I tried printing out the values but I just get a single value repeated infinitely. What's going on? Much help would be appreciated. In main, this is what I have:

    DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>();
    list.add(0, 1);
    list.add(1, 2);
    for (Integer i : list) {
        System.out.print(i + " ");
    }

Here is the code for the Node Class:

public class Node<E> {

private E data;
private Node<E> next;
private Node<E> prev;

public Node(E data) {
    //do I need this?
    this.data = data;
}

@Override
public String toString() {
    //TODO
    return data + " ";
}
// Implement Methods
public E getData() {
    return data;
}
public Node<E> getNext() {
    return next;
}
public Node<E> getPrev() {
    return prev;
}

public void setData(E e) {
    data = e;
}
public void setNext(Node<E> e) {
    next = e;
}
public void setPrev(Node<E> e) {
    prev = e;
}

}

EDIT: Here is my add method:

    public boolean add(int index, T data) {
    // TODO
    boolean toReturn = false;
    if (data == null) return false;
    if (index == 0) {
        Node<T> toAdd = new Node<T>(data);
        if (isEmpty()) {
            head = toAdd;
            tail = toAdd;
        } else {
            head.setPrev(toAdd);
        }
        toAdd.setNext(head);
        head = toAdd;
        toReturn = true;
    } else if (index == size()) {
        Node<T> toAdd = new Node<T>(data);
        if (isEmpty()) {
            head = toAdd;
            tail = toAdd;
        } else {
            tail.setNext(toAdd);
            toAdd.setPrev(tail);
        }
        tail = toAdd;
        toReturn = true;
    } else {
        Node<T> toAdd = new Node<T>(data);
        if (isEmpty()) {
            head = toAdd;
            tail = toAdd;
        } else {
            getNodeAt(index).setPrev(toAdd);
        }
        toAdd.setNext(getNodeAt(index));
        getNodeAt(index-1).setNext(toAdd);
        toReturn = true;
    }
    size++;
    return toReturn;
}

The following is my getNodeAt() method:

    private Node<T> getNodeAt(int index) {
    int count = 0;
    Node<T> element = head;
    while (element !=  null) {
        if (count == index) {
            return element;
        } else {
            count++;
            element = element.getNext();
        }
    }
    return null;
}
7
  • 4
    Can you post the code for the DoublyLinkedList please. Commented Feb 10, 2014 at 19:22
  • 1
    What's with the weird casting in the constructor? Commented Feb 10, 2014 at 19:26
  • Do you have implemented equals and hashcode in your DoublyLinkedList? Commented Feb 10, 2014 at 19:27
  • I just updated my code Commented Feb 10, 2014 at 19:43
  • Please post your DoublyLinkedList class Commented Feb 10, 2014 at 20:01

2 Answers 2

1

I am willing to bet your problem is in the Node (which you dont supply the code). Check that Node.getNext() is actually returning the next node and not a reference to itself.

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

4 Comments

does your add method appropriately call the setNext()/setPrevious() method?
I think so. i have supplied my add() method above. It removes based off of if it needs to be added to the front, middle or end.
Tony the Pony is correct that you don;t call setPrevious on toAdd. Also check the isEmpty() method. Also, if index=size(), you need to setNext() on the node tail.getPrevious();
do you still have the infinite loop when you list.add(0,1); list.add(0,2) ? This way you never use the second part of the if-then. Also, make sure that index is not larger than size().
0

You have a bug in your add method. When an element is inserted in the middle of the list, your code fails to set the link to the previous sibling of toAdd.

As a result, somewhere in the flow of your program, adding and removing elements might result in the list becoming corrupt.

That's my best guess without looking at the rest of your code.

1 Comment

But wouldn't that be irrelevant in this case considering that I am adding to empty list which would trigger an insertion to the front (1st if case), and then another addition which would insert to the back (2nd if case). The else (middle insertion) shouldn't be running. On a side note, I also realized that my second condition should be: else if (index == size()-1) and I have changed that and it still hasn't worked.

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.