简体   繁体   English

LinkedLIst indexOf递归实现

[英]LinkedLIst indexOf recursive implementation

I'm having a hard time understanding how the recursion in following implementation of indexOf() of LinkedList works: 我很难理解LinkedListindexOf()的后续实现中的递归是如何工作的:

/**
 * Returns the position of the first occurrence of the 
 * given character in this list, or -1 if there is no 
 * such occurrence.
 */
public int indexOf(char c)
{
    if (this.head == null)
    {
        return -1;
    }
    else
    {
        return LinkedList.indexOf(c, this.head);
    }
}
private static int indexOf(char c, Node node)
{
    if (node.data == c)
    {
        return 0;
    }
    if (node.next == null)
    {
        return -1;
    }
    int index = LinkedList.indexOf(c, node.next);
    if (index == -1)
    {
        return -1;
    }
    else
    {
        return 1 + index;
    }
}

where head is the initial node ("link") in the list. 其中head是列表中的初始节点(“link”)。

The static indexOf() method checks whether there's a data match in the head, and then whether there exists a next node. static indexOf()方法检查头部是否存在数据匹配,然后检查是否存在下一个节点。 Assume both conditions are false (there's no data match and there exists a next node). 假设两个条件都是false (没有数据匹配,并且存在下一个节点)。 Then indexOf() is called recursively on the next (second) node to perform the same checks. 然后在下一个(第二个)节点上递归调用indexOf()以执行相同的检查。 However, if both conditions are still false , what does it return to int index given that there's no else clause before index is declared? 但是,如果两个条件仍为false ,那么在声明index之前没有else子句的情况下它返回int index index是什么? That doesn't seem legal. 这似乎不合法。

When I stepped through it in the debugger, rather than returning anything it actually seems to do more recursive calls until one of the if conditions is met (either there's a data match or the end of the list is reached). 当我在调试器中逐步执行它时,而不是返回任何东西,它实际上似乎做了更多的递归调用,直到满足if条件之一(数据匹配或列表的末尾到达)。 But why would it do that? 但为什么会这样呢? And then how is 1 being added to the index each time? 然后每次如何将1添加到index

However, if both conditions are still false, what does it return to int index given that there's no else clause before index is declared? 但是,如果两个条件仍为false,那么在声明index之前没有else子句的情况下它返回int索引是什么?

It will return nothing yet, it will enter another level of recursion: 它还没有返回,它将进入另一个递归级别:

indexOf(node) -> indexOf(node.next) -> indexOf(node.next.next) -> ...

Then when the conditions are met it finally returns a value and you start to come back from each recursion call you made before: 然后,当满足条件时,它最终返回一个值,然后您开始从之前进行的每个递归调用返回:

indexOf(node) <- indexOf(node.next) <- indexOf(node.next.next) <- ...

but doing so it will also add 1 to the index it is returning, therefore basically counting the recursion levels you reached which equal to the node number you reached which is the index you are looking for. 但是这样做它也会在它返回的索引中加1,因此基本上计算你到达的递归级别,它等于你达到的节点数,这是你要查找的索引。

This diagram shows how the algorithm works when there is a match on the fourth node. 此图显示了当第四个节点上存在匹配时算法如何工作。 The blue color indicates that we are entering recursion, the red color indicates that we are coming back from recursion (click the image to open it in its original size): 蓝色表示我们正在输入递归,红色表示我们从递归返回(单击图像以原始大小打开它):

递归

This other diagram shows the order of execution of the instructions in case of a match on the third node (click the image to open it in its original size): 此另一个图显示了在第三个节点上匹配时指令的执行顺序(单击图像以原始大小打开它):

递归,代码执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM