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

private T element;

public LinearNode(T elem) {

next = null;

element = elem;

}

public LinearNode getNext() {

return next;

}

public void setNext(LinearNode node) {

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(LinearNode node, T target) (Hint: use .equals to compare the target with the contents of each node.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!