Hey everyone...

Well I am sorting items based upon a number.

My first case is if tail == null, if so that the first element to be added in the list.

My second case is if the element I want to add number is greater than the tail. If so I will set that element as the new tail.

My third case if where I get the infinite loop.
Okay, the first element(a Node)'s data is 5, then new number I want to add to the list is 4 .

``````while(info.compareTo(curr.getData()) < 0)
{
curr= curr.getNext();
}``````

So 5, points backs to itself right now.

It will go into the loop and then into the compare method.

Is 4 > 5? No, return -1
is -1 < 0 True
currr = curr.getNext() points back to 5(Since its circular)
Loop

Is 4 > 5? No, return -1
is -1 < 0 True
currr = curr.getNext() points back to 5(Since its circular)
Loop

Is 4 > 5? No, return -1
is -1 < 0 True
currr = curr.getNext() points back to 5(Since its circular)
Loop

Is 4 > 5? No, return -1
is -1 < 0 True
currr = curr.getNext() points back to 5(Since its circular)
Loop

Is 4 > 5? No, return -1
is -1 < 0 True
currr = curr.getNext() points back to 5(Since its circular)
Loop

And so on.........................

I ran through the debugger three times before I realized it.
Any help?

## All 2 Replies

Ok, so why does `curr = curr.getNext()` point to itself, exactly? Do you only have one element in your list?

Yes I only have one element in the list, and it is supposed to get the next node when I have more than on element in the list.

``````public void add(Phone detail)
{
Node temp = tail;
if(tail == null)
{
Node newNode = new Node(detail, tail, tail);
tail = newNode;
tail.setNext(tail);
tail.setPrev(tail);
}
else if(detail.compareTo(temp.getPhoneData()) > 0)
{
Node newNode = new Node(detail, tail, tail.getPrev());
tail.getPrev().setNext(newNode);
tail.setPrev(newNode);
tail = newNode;
}
else
{
while(detail.compareTo(temp.getPhoneData()) < 0)
{
temp = temp.getNext();
}

Node toAdd = new Node(detail, temp.getNext(), temp);