递归函数保持运行,不打印任何内容



长话短说,我应该制作一个代码,在跳过列表中插入、删除、搜索和打印数字,第一个节点为负无穷大,最后一个节点为正无穷大(-inf>(…(>inf(。我从插入函数调用了搜索函数,以找到一个插入任何新节点的位置(仅在插入第三个节点之后(,并在主函数之外而不是主函数内部初始化或引用我的节点(尽管我正在讨论是否应该改为执行后者(。然而,我的一个函数可能会陷入循环。

static Node search(double item, double max) {
Node head2 = head;
head2 = Start(head2, max);
//starts at the first highest node in the skiplist
//{... } //find a specific node in a skiplist
return head2;
}
//find first highest node for the search function
static Node Start(Node head2, double max) {
System.out.println(head.key + " " + head.level);
Node s = new Node();
if (head2.max < max) {
s = Start(head2.next, max);
return s;
}
else if (head2.max >= max && head2.inf == false) {
if (head2.level < head2.max) {
s = Start(head2.up, max);
return s;
}
else if (head2.level == head2.max) {
s = head;
return s;
}
}
return s;
}

start函数是从搜索函数调用的(按main>double insert>Node search>Node start的顺序调用(,它应该找到最高级别的第一个节点。一旦这样做,它就会将该节点返回到搜索函数,以便从那里开始搜索。但当被调用时,它只是一片空白,尽管继续运行,但什么也没发生。当我输入一个打印函数来确定问题时,它只打印第一个节点的键和第一个级别,然后变为空白。UPDATE:我了解到函数能够找到节点,但不能通过递归返回。我想找到一种方法来解决这个问题。

问题实际上是在我的搜索函数中。

for(j = max; j >= 1; j--) {
while(head2.next != last && head2.key != item && i == 0) {
if(item > head2.key && head2.next != last) {
head2 = head2.next;
}
else if(item < head2.key || head2.next == last) {
head2 = head2.prev;
i = 1;
}
}
(...)}

这是一个一直循环的函数,所以我不得不让它这样说来更改语句一段时间while(head2.next!=last&&head2.key<item&&head2.inf!=true&&i==0(

最新更新