递归功能保持运行并且不打印任何内容 [英] recursion function keeps running and prints nothing

查看:91
本文介绍了递归功能保持运行并且不打印任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Long story short, I'm supposed to make a code that inserts, deletes, searches for and prints numbers in a skip list with the first node being negative infinity and the last node being positive infinity (-inf > (...) > inf). I called my search function from my insert function to find a spot to insert any new nodes (only after the third node has been inserted) and I initialize or reference my nodes outside the main function rather than inside of it (although I'm debating on whether or not I should do the latter instead). However one of my functions may be stuck in a loop.

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>两次插入>节点搜索>节点开始),它应该找到最高级别的第一个节点。完成此操作后,它将使该节点返回搜索功能,以便可以从那里开始搜索。但是当被调用时,它只是一片空白,尽管继续运行也没有任何反应。当我使用打印功能确定问题时,它只打印第一个节点的密钥和第一个级别,然后从那里空白。更新:我了解到该函数可以找到该节点,但无法通过递归返回它。我想找到一种解决方法。

The start function is called from the search function (called in order of main > double insert > Node search > Node start) and it is supposed to find the first node of the highest level. Once it does so, It returns that node to the search function so it can start it's search from there. But when called, it simply goes blank and nothing happens despite continuing to run. When I put in a print function to determine the problem, it simply prints the first node's key and 1st level and goes blank from there. UPDATE: I've learned that the function is able to find the node but it can't return it via recursion. I would like to find a way to fix that.

推荐答案

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

The problem was actually in my search function.

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)

this was the function that kept looping so I had to change the statement for while by having it say this instead while(head2.next != last && head2.key < item && head2.inf != true && i == 0)

这篇关于递归功能保持运行并且不打印任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆