gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:44 25 4
gpt4 key购买 nike

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

代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com