有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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的顺序调用),它应该查找最高级别的第一个节点。一旦这样做了,它就会将该节点返回到搜索函数,以便从那里开始搜索。但当被调用时,它只是变为空白,尽管继续运行,但什么也没有发生。当我输入打印函数来确定问题时,它只打印第一个节点的键和第一个级别,然后从那里变成空白。更新:我了解到函数能够找到节点,但不能通过递归返回。我想找到一个方法来解决这个问题


共 (1) 个答案

  1. # 1 楼答案

    问题其实出在我的搜索功能上

    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)