java - 如何在 Java 中递归地对节点中的项目求和?

标签 java recursion

我正在尝试以迭代和递归方式对节点中的项目求和。我已经编写了以迭代方式执行的程序,但我对如何以递归方式完成此操作有疑问。

代码:

public int sumIterative() {
    Node newNode= new Node(item, next);
    int sum = 0;

    while(newNode != null){
        sum = sum + item;
        newNode = newNode.next;
    }
    return sum;
}

我在递归方式上的努力:

public int sumRecursive() {
    Node newNode = new Node(item,next);
    int sum = 0;
    int result;
    if(newNode == null){
        result = 0;
    } else {
        return sumRecursive();
    }
    return sum;    
}

我试图用“此后没有节点”的逻辑来实现这一点

这怎么可能递归完成?

编辑

这是公共(public)驱动方法,我可以按需发布我的整个代码

public int sumRecursive() {
    int sum = 0;
    if (top != null) {
        sum = top.sumRecursive();
    }
    return sum;
}

class LinkedList {

    private class Node {
        public int item;
        public Node next;

        public Node(int newItem, Node newNext)  {
            item = newItem;
            next = newNext;
        }

        public int getItem() {
            return item;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node newNext) {
            next = newNext;
        }

        // My Other Node methods

        // Recursively add up the numbers stored in this
        // and all the nodes after this.
        // Base case: there are no nodes after this.
        public int sumRecursive() {
            Node node = new Node(item,next);

            /*Node newNode = new Node(item,next);
            int sum = 0;
            if (null == newNode) {
                return sum;
            } else {
                sum += sumRecursive(newNode.next);
            }*/

            if (node == null) {
                return node.item;
            } else {
                return node.item + sumRecursive(node.next);
            }
        }

        // Iteratively add up the numbers stored in this
        // and all the nodes after this.
        public int sumIterative() {
            Node newNode= new Node(item, next);
            int sum = 0;

            while(newNode != null) {
                sum = sum + item;
                newNode = newNode.next;
            }
            return sum;
        }
    } // end class Node

    private Node top; // a pointer to the first node in the list (when it's not empty)

    public LinkedList() {
        top = null; // empty list
    }

    // Insert a node at the front of the list
    public void insertAtFront(int newItem)  {
        top = new Node(newItem, top);
    }

    // Print out the list (10 numbers per line)
    public void printList() {
        int count = 1;
        Node curr;

        if (top == null) {
            System.out.println( "List is empty" );
        } else {
            curr = top;
            while (curr != null) {
                System.out.print( " " + curr.item );
                count++;
                if (count % 10 == 0) {
                    System.out.println();
                }
                curr = curr.next;
            }
            if (count % 10 != 0) {
                System.out.println();
            }
        }
    }

    // public driver method for sumRecursive
    public int sumRecursive() {
        int sum = 0;

        if (top != null) {
            sum = top.sumRecursive();
        }

        return sum;
    }

    // public driver method for sumIterative
    public int sumIterative() {
        int sum = 0;

        if (top != null) {
            sum = top.sumIterative();
        }

        return sum;
    }
} 

最佳答案

public int sumRecursive() {
  return sumRecursive(top);
}    

public int sumRecursive(Node node){
  if (node == null) {
    return 0;
  }
  return node.item + sumRecursive(node.next);
}

//或匹配你的驱动函数

public int sumRecursive() {
  if (top == null) {
    return 0;
  } else {
    return top.sumRecursive();
  }
}    

// in Node class    
public int sumRecursive(){ 
  int sum = this.item;  
  if (this.next != null) {
    sum += this.next.sumRecursive();
  }
  return sum;
}

关于java - 如何在 Java 中递归地对节点中的项目求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35138336/

相关文章:

java - 什么是 Java 微服务

java - 使用 getName() 时如何摆脱文件类型?

javascript - 如何截取整个网页的屏幕截图?

c++ - 使用递归查找数组中字符出现的次数

loops - 函数式编程中的无限循环?

php - 使用php计算关联数组中元素的深度

Java ArrayList - 我如何判断两个列表是否相等,顺序无关紧要?

Java + MySQL 完整性违规处理

python-3.x - while 循环和使用 after 重复调用函数的区别

c++ - 对具有按引用传递参数的函数使用递归?