Question
Implement a recursive method called countOccurrences that takes the rst node in a singly linked list and returns the number of times a particular value
Implement a recursive method called countOccurrences that takes the rst node in a singly linked list and returns the number of times a particular value occurs in the list. For example, if countOc- currences("A") is called on a list with nodes containing: "A", "B", "C", "B", "E", it would return 1. If countOccurrences("B") was called on the same list, it would return 2. For reference, a standard implementation for the nodes of a singly linked list is given below.
public class LinearNode
private LinearNode
private T element;
public LinearNode(T elem) {
next = null;
element = elem;
}
public LinearNode
return next;
}
public void setNext(LinearNode
next = node;
}
public T getElement() {
return element;
}
public void setElement(T elem) {
element = elem;
}
(a) Using the fantastic four approach, determine the size n problem for the method countOccurrences. (b) Identify the stopping condition(s) and the return value, if any, for the problem.
(c) Determine the size m problem(i.e. the subproblem) for the problem.
(d) How is the size-n problem constructed from the size m problem?
(e) Implement the recursive method public static int countOccurrences
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