代码之家  ›  专栏  ›  技术社区  ›  Snowman

Java中的递归与链表

  •  1
  • Snowman  · 技术社区  · 16 年前

    好吧,假设我有一个函数在自定义LinkedList类中查找特定单词:

    public LinkedList find(String word) {
        if (this.word.equals(word))
            return this;
        if (next==null)
            return null;
        if (next.find(word)==next)
            return next;
        return null;
    }
    

    编辑:这个代码有什么问题:

    public LinkedList findLast(String word) {
        LinkedList temp=new LinkedList(word, null);
        if (next==null && next.word.equals(word))
            return next;
        if (next==null && !next.word.equals(word))
            temp=next.findLast(word);
        return temp;
    }
    
    7 回复  |  直到 16 年前
        1
  •  8
  •   Jon Skeet    16 年前

    好吧,这样想:你需要递归 正确的 到列表的末尾,然后让返回值冒泡起来。

    因此,方法的开始应该是递归调用,以便进一步向下查看列表,或者注意我们在列表的末尾,这相当于“进一步”结果为空。

    现在,当您返回时,有三个选项:

    • 你已经 找到匹配项(因此递归调用的返回值为空)并且:
      • 当前点的单词匹配-因此返回当前点
      • 当前点不匹配-因此返回null

    希望这应该足够让你实现-如果不是,请问更多的问题。我不想在这可能是家庭作业的时候给出一个完整的实现。

        2
  •  3
  •   Bane    16 年前

    存储对找到的最新引用的引用,并继续调用自身,直到它返回空值,然后返回最新引用。

    请注意,需要澄清的是:您必须遍历整个链接列表(除非您有一个双链接列表)才能实现这一点--每次找到匹配项时存储一个引用(但每次都覆盖相同的引用),然后在到达列表末尾时返回引用保留的内容。

    public class LinkedList {
    
      private static int uniqueIdCounter = 0;
    
      private final String word;
      private int uniqueId;
      private LinkedList next = null;
    
      public LinkedList( String word ) {
    
        this.word = word;
        this.uniqueId = uniqueIdCounter++;
      }
    
      @Override
      public String toString() {
    
        return this.word + "(" + this.uniqueId + ")";
      }
    
    
    
      public void setNext( LinkedList next ) {
    
        this.next = next;
      }
    
      public LinkedList find( String word ) {
    
        return this.find( word, null );
      }
    
      public LinkedList find( String word, LinkedList result ) {
    
        if( this.word.equals( word ) ) {
            result = this;
        }
    
        if( this.next != null ) {
    
            result = this.next.find(word, result);
        }
    
        return result;
      }
    
      public static void main(String[] args) {
    
        LinkedList head = new LinkedList( "A");
        System.out.println( "Head is: " + head );
    
        LinkedList B = new LinkedList( "B" );
        head.setNext( B );
        System.out.println( "B is: " + B );
    
        LinkedList A2 = new LinkedList( "A" );
        B.setNext( A2 );
        System.out.println( "A2 is: " + A2 );
    
        LinkedList last = head.find( "A" );
        System.out.println( "Last is: " + last );
      }
    
    }
    

    结果如下:

    头部:A(0)

    B是:B(1)

    A2是:A(2)

    最后是:A(2)

        3
  •  2
  •   Kel    16 年前

    对于某些有用的操作,每个直接递归函数都有两个位置:在进一步调用方法之前和之后:

      function(n){
        doBefore(n);
        function(n+1)
        doAfter(n)
      }
    

    doBefore()在“前进的路上”执行,doAfter()在“后退的路上”执行。现在你的算法在前进的路上检查单词的相等性。您必须修改算法,以便在返回时执行此检查。

        4
  •  1
  •   Peter Knego    16 年前
    public LinkedList find(String word, LinkedList result) {
       if (this.word.equals(word))
            result = this;
       if (next != null )
            return next.find(word, result)
       return result;
    

    public LinkedList find(String word, LinkedList result) {
         result = this.word.equals(word) ? this : result;
         return next == null ? result : next.find(word, result);
    

    @是的,解释:记住结果,用后面的结果替换,最后返回。

    具有一个参数的方法:

    public LinkedList find(String word){
       result = this.word.equals(word) ? this : null;
       if(next != null)
          previous = next.find(word);
          return (previous != null) ? previous : result 
       else 
          return result;
    
        5
  •  0
  •   bhavinp    16 年前

    从尾部向后跑。

    public LinkedList find(String word) {
            if (this.word.equals(word))
                return this;
            if (prev==null)
                return null;
            if (prev.find(word)==prev)
                return prev;
            return null;
    }
    
        6
  •  0
  •   ILMTitan    16 年前

    你的第一个假设是完美的。这是你成功的基本案例。

    你的第二个if语句也是完美的。这是你的失败基础。

    对于findLast(String word),我不能对Jon Skeet说的话添加太多内容。关于唯一的建议,我可以添加它,永远不要让a节点检查它的邻居。每个节点应该只检查自己。你应该多看看 this.word.equals(word) next.word.equals(word) .

        7
  •  0
  •   khachik    16 年前

    public LinkedList find(String word) {
      if(this.word.equals(word)) return this;
      return next==null?null:next.find(word);
    }
    
    

    public LinkedList rfind(String word) { if(next != null) { LinkedList res = next.rfind(word); if(res != null) return res; } return this.word.equals(word)?this:null; }