Java 递归大小法链表

Java 递归大小法链表,java,recursion,linked-list,Java,Recursion,Linked List,我试图理解我从rercusive链表大小方法收到的输出 private int size(Node list) { if (list == null) return 0; else { int results = size(list.next) + 1; System.out.println(results); return results; } } 我在运行时收到的输出如下: 一,

我试图理解我从rercusive链表大小方法收到的输出

private int size(Node list)
{
   if (list == null)   
       return 0;
   else 
   {
      int results = size(list.next) + 1; 
      System.out.println(results);
      return results;
   }             
}
我在运行时收到的输出如下:

一, 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. 五,

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

一, 2. 3. 4. 五,

为什么会这样

我注意到当我这样添加元素时
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());
这是我正在使用的递归add方法

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;     
试试这个:

  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<0 | | index>size())
条件的
System.out.println(results)在您的大小方法中。事实上,它的方法是在add函数中调用的,由于您的输出是从“1,2”开始的,您可以看到问题出现在main中对add方法的第三次调用上。尝试删除您的
System.out.println(结果)并写入main
System.out.println(ll.size())
,输出为5。

是否也可以发布节点类?
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