Question
Need help with these! 1) A) Perform recursive analysis for for the problem of summation. Our goal is to implement a method called sumList that
Need help with these!
1)
A)
Perform recursive analysis for for the problem of summation. Our goal is to implement a method called sumList that takes a reference to the first node (e.g., head) in a list of integers and returns the sum of its contents. By convention, the sum of an empty list will be zero. (You may want to look at the source code in the next question).
Using the fantastic four approach, determine the size n problem (i.e. the whole problem) for the method sumList.
Identify the stopping condition(s) and the return value, if any, for method sumList.
Determine the size m problem (i.e. the subproblem) for the method sumList.
How does is the size-n problem constructed from the size m problem?
B)
Give a recursive implementation of sumList. A method signature is given below.
public class IntNode {
private int value;
private IntNode next;
public IntNode(int i) { value = i; }
public int getInt() { return value; }
public void setInt(int v) { value = v; }
public IntNode getNext() { return next; }
public void setNext(IntNode n) { next = n; }
}
public class Sum {
public static void main(String[] args) {
IntNode a = new IntNode(10);
//omitted: code appending more new elements to the list.
int sum = sumList(a);
System.out.println("The sum is " + sum);
}
//TODO: implement recursive sum implementation.
public static int sumList(IntNode head) {
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started