递归函数保持运行,不输出任何内容

问题描述 投票:0回答:1

长话短说,我应该编写一个代码,以在第一个节点为负无​​穷大而最后一个节点为正无穷大(-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;
}

开始功能是从搜索功能中调用的(按主顺序>双重插入>节点搜索>节点启动的顺序调用),并且应该找到最高级别的第一个节点。一旦这样做,它将使该节点返回搜索功能,以便它可以从那里开始搜索。但是,当被调用时,它只是一片空白,尽管继续运行也没有任何反应。当我使用打印功能确定问题时,它只打印第一个节点的密钥和第一个级别,然后从那里空白。更新:我了解到该函数能够找到该节点,但无法通过递归返回它。我想找到一种解决方法。

java recursion nodes doubly-linked-list skip-lists
1个回答
0
投票

问题实际上出在我的搜索功能中。

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!=最后一个&& head2.key

© www.soinside.com 2019 - 2024. All rights reserved.