【问题标题】:recursive size method linked list递归大小方法链表
【发布时间】:2016-11-13 18:04:27
【问题描述】:

我正在尝试了解我从递归链表大小方法收到的输出。

private int size(Node list)
{
   if (list == null)   
       return 0;
   else 
   {
      int results = size(list.next) + 1; 
      System.out.println(results);
      return results;
   }             
}

我在运行时收到的输出是这样的:

1 2 1 2 3 1 2 3 1 2 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5

它计算出正确的答案,但它产生的递归调用比我预期的要多。我期待这样的输出。

1 2 3 4 5

这是为什么?

我注意到当我以这种方式添加元素ll.add("Amy"); ll.add("Bob") 时,我收到了我期待的输出,但是当我以这种方式添加元素ll.add(0, "Al"), ll.add(2, "Beth"), ll.add(4, "Carol") 时,它会产生我没想到的输出。我想弄清楚为什么输出看起来像这样,有什么想法吗?

 public static void main(String [] args)
 {
    RLinkedList ll = new RLinkedList();
    ll.add("Amy");
    ll.add("Bob");
    ll.add(0, "Al");
    ll.add(2, "Beth");
   ll.add(4, "Carol");
    System.out.println(ll.size());

这是我正在使用的递归添加方法。

public void add(String e)
{
   // Replace first with result of adding e to first 
   first = add(e, first);
}

/**
   This recursive private add method adds
   an element e to the end of a list.
   @param e The element to add to the list.
   @param list The list to add e to.
   @return The list resulting from adding e to its end.
*/

private Node add(String e, Node list)
{
   if (list == null)
   {
       // Base case
       return new Node(e);
   }
   else
   {
       // Add e to the end of the tail and use
       // the result to replace the tail
       list.next = add(e, list.next);
       return list;
   }        
}

/**
   The add method adds an element e at place index
   in this linked list.
   @param index The place in the list to add an element.
   @param e The element to add this the linked list.
     @exception IndexOutOfBoundsException When index is 
              out of bounds.  
*/

public void add(int index, String e)
{
   // Replace first with the result of adding
   // e at index in first
   first = add(index, e, first);        
}    

/**
   This add method adds an element at an index in a list.
   @param e The element to add to the list.
   @param index The index at which to add the element.
   @param list The list to add e to.
   @return The list resulting from adding e.
   @exception IndexOutOfBoundsException When index is 
              out of bounds.  
*/

private Node add(int index, String e, Node list)
{
    if (index < 0  || index > size()) 
    {
         String message = String.valueOf(index);
         throw new IndexOutOfBoundsException(message);
    }         
    if (index == 0)        
         return new Node(e, list);        

    // 0 < index and index <= size so list is not empty
    // Replace the tail with result of adding e at index - 1
    // in the tail

    list.next = add(index-1, e, list.next);        
    return list;     

这是节点类

private class Node
{
    String value;   
    Node next;      

    /**
       Constructor.            
       @param val The element to store in the node.
       @param n The reference to the successor node.
    */

    Node(String val, Node n)
    {
        value = val;
        next = n;
    } 

    /**
       Constructor. 
       @param val The element to be stored in the node.
    */

    Node(String val)
    {
       // Just call the other (sister) constructor
       this(val, null);            
    }
}   

【问题讨论】:

  • 你能把Node类也发一下吗?

标签: java recursion linked-list


【解决方案1】:

试试这个:

  public static void main(String[] args) {
    RLinkedList ll = new RLinkedList();
    ll.add("Amy");
    ll.add("Bob");
    ll.add(0, "Al");
    ll.add(2, "Beth");
    ll.add(4, "Carol");

    System.out.println("SIZE");
    System.out.println(ll.size());
  }

你会看到你得到了

1 2 1 2 3 1 2 3 1 2 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
SIZE: 
1
2
3
4
5
5

当您调用 add(index, "string") 时,所有额外的输出都来自您的 if (index &lt; 0 || index &gt; size()) 条件

【讨论】:

    【解决方案2】:

    这只是您的尺寸方法中的System.out.println(results);。实际上是在 add 函数中调用的方法,并且由于您的输出从“1, 2”开始,您可以看到问题出在 main.add 方法的第三次调用上。尝试从 size 函数中删除您的 System.out.println(results); 并写入您的 main System.out.println(ll.size()) ,输出将为 5。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多