我正在尝试为算法课程编写一个跳过列表,而我的传播方法遇到了问题(一旦在A_prop和B_prop之间找到了插入点,就会调用该问题)。这是代码:
/* The randomized propagation upwards */
private void propagate(SkipNode<T> A_prop, SkipNode<T> B_prop,
SkipNode<T> front_prop, SkipNode<T> end_prop,
SkipNode<T> insert)
{
SkipNode<T> clone, rowBelow;
Random random = new Random();
float f = random.nextFloat();
int flip = Math.round(f);
A_prop.linkRight(insert);
B_prop.linkLeft(insert);
rowBelow = insert;
System.out.println("Flip = " + flip);
while(flip != 0);
{
System.out.println("HEADS");
clone = insert.clone();
// Insert node at this level
A_prop.linkRight(clone);
B_prop.linkLeft(clone);
clone.linkDown(rowBelow);
System.out.println("After Link");
System.out.println(A_prop.right);
System.out.println(insert);
System.out.println(insert.right);
System.out.println(B_prop);
if(A_prop.up != null)
{ // Move up a row
System.out.println("A up");
A_prop = A_prop.up;
B_prop = A_prop.right;
front_prop = front_prop.up;
end_prop = end_prop.up;
rowBelow = clone;
}
else if(front_prop.up != null)
{
System.out.println("Front up");
while(A_prop.up == null)
{ // Find the closest node to the left that
// has a copy in the above level
A_prop = A_prop.left;
}
A_prop = A_prop.up;
B_prop = A_prop.right;
front_prop = front_prop.up;
end_prop = end_prop.up;
rowBelow = clone;
}
else // Create new top level
{
System.out.println("New row");
// Make new terminal nodes
A_prop = new SkipNode<T>("HEAD");
B_prop = new SkipNode<T>("TAIL");
// Link them to the current ones
front_prop.linkUp(A_prop);
end_prop.linkUp(B_prop);
// Update current
front_prop = front_prop.up;
end_prop = end_prop.up;
// Link 'em
front_prop.linkRight(end_prop);
// To be sure
A_prop = front_prop;
B_prop = end_prop;
rowBelow = clone;
}
f = random.nextFloat();
flip = Math.round(f);
}
// Don't forget to update the global head and tail for the list
head = front_prop;
tail = end_prop;
}
我仍然有一些调试用的println();我在控制台上得到的输出是以下两种情况之一:
翻转= 1
翻转= 0
头
在案例1中,程序仍在运行(我需要点击控制台上的红色小方框以中断),但是没有更多的代码执行。我把println()放在了while循环之后,而没有打印出来,并且我已经注释掉了循环中的所有内容-没有效果。在重新启动Eclipse之前,我的计算机运转异常嘈杂。
在第2种情况下,程序继续运行,直到遇到第一种情况为止。
所有四种链接方法均已测试并且可以工作。我尝试过重新启动计算机,甚至还尝试重新安装Java(版本1.7.0_45)。我不知道是什么原因造成的,我们将不胜感激。
谢谢!
请查看以下代码行...不应使用任何分号来终止while循环!!!
while(flip != 0);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句