【问题标题】:How to sum items in a Node recursively in Java?如何在Java中递归地对节点中的项目求和?
【发布时间】:2016-02-01 19:09:55
【问题描述】:

我正在尝试以迭代方式和递归方式总结节点中的项目。我已经编写了执行迭代方式的程序,但我在如何递归完成时遇到了问题。

代码:

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 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;
    }
} 

【问题讨论】:

  • 这个方法是在某个LinkedList类型的类中定义的吗?否则,top 是什么?
  • 我更新了它@cricket_007

标签: java recursion


【解决方案1】:
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;
}

【讨论】:

    【解决方案2】:
        public int sumRecursive(Node node){
            if(node == null){
                return 0;
            }else{
                return node.item + sumRecursive(node.next);
            }   
        }
    

    【讨论】:

    • 什么是null的node.item?
    • 我遇到了这个问题,因为公共驱动方法与此不同,我进行了编辑,现在应该显示了
    • 如果第一个元素为空怎么办?
    • 越来越好。测试node.next==null 仍然没有意义(除了保存一个递归调用)。
    • 这很尴尬...完全失焦了。
    猜你喜欢
    • 2022-06-21
    • 1970-01-01
    • 2020-11-04
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多