【发布时间】:2016-04-16 00:08:42
【问题描述】:
这种插入排序属于双向链表。它似乎没有打印出任何东西。抱歉,如果这很混乱。我对发布东西还很陌生。我已经通过放入 sysout 对其进行了调试。我相信我使用 sysout 的交换存在问题,我注意到这就是问题发生的地方。任何帮助将不胜感激。我还检查了我的应用程序类,看起来不错。
public boolean insertionSort()
{
if (getFirst().next != null)
{
return false;
}
Link current = getFirst().next;
Link current2 = current;
while(current != null){
current2 = current;
while(current2.prev != null){
int tempID = Integer.valueOf(current2.Data.getID());
int temp2ID = Integer.valueOf(current2.prev.Data.getID());
if(tempID < temp2ID )
{
swap(current2, current.prev);
}
current2 = current2.prev;
}
current = current.next;
}
return true;
}
public void swap(Link x, Link y)
{
Link previousNode1 = x.prev;
Link nextNode1 = x.next;
Link previousNode2 = y.prev;
Link nextNode2 = y.next;
if (x.next == y || y.next == x)
{
previousNode1.next = y;
y.prev = (previousNode1);
nextNode2.next = (x);
x.next = (nextNode2);
x.prev = (y);
y.next = (x);
}
else
{
y.prev = (previousNode1);
y.next = (nextNode1);
nextNode1.prev = (y);
previousNode1.next = (y);
x.prev = (previousNode2);
x.next = (nextNode2);
nextNode2.prev = (x);
previousNode2.next = (x);
}
} // end swap
【问题讨论】:
标签: java doubly-linked-list insertion-sort