我试图理解我从我的rerusive链表大小方法中收到的输出。
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;
这是Node类
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);
}
}
答案 0 :(得分: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
当您拨打if (index < 0 || index > size())
add(index, "string")
条件
答案 1 :(得分:0)
这只是你的尺寸方法中的System.out.println(results);
。实际上它是在add函数中调用的方法,并且,因为你从“1,2”开始输出,你可以看到问题是在你的main中第三次调用add方法。尝试从size函数中删除System.out.println(results);
并在主System.out.println(ll.size())
中写入,输出将为5.