做作业问题,插入链表的第一项插入很好,当我插入更多值时,它们出现乱序,因为根据调试器 current.next 仍然 == null,我无法弄清楚为什么我的一生.
public void insert(String key)
{
Link newLink = new Link(key);
Link current = first;
Main.nodeCount++;
while(current != null && key.compareTo(current.dData) > 0)
{
if(current.next != null)
current = current.next;
else
break;
} // end while
if(isEmpty())
{
first = newLink;
last = newLink;
return;
}
if (current == first )
{
if(key.compareTo(current.dData) < 0)
{
newLink.next = current;
current.previous = newLink;
first = newLink;
return;
}//end if
if(key.compareTo(current.dData) > 0)
{
current.next = newLink;
first.next = newLink;
newLink.previous = current;
return;
}//end if
}
if (current == last)
{
if(key.compareTo(current.dData) < 0)
{
current.previous.next = newLink;
newLink.previous = current.previous;
newLink.next = current;
current.previous = newLink;
last = current;
}
if(key.compareTo(current.dData) > 0)
{
newLink.previous = current;
current.next = newLink;
last = newLink;
return;
}//end if
return;
}//end if
if (current != first && current != last)
{
current.previous.next = newLink;
newLink.previous = current.previous;
newLink.next = current;
current.previous = newLink;
}