QUESTION 18 countOccurrencesr.A\"is called on a list with nodes containing: ?A\"
ID: 3894458 • Letter: Q
Question
QUESTION 18 countOccurrencesr.A"is called on a list with nodes containing: ?A","B","C".-?'·'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 t private LinearNode next private T element public LinearNode(T elem) ( next-nullt element-elem) public Linear Node getNexto return next) public vold setNext LinearNode node) next- node:) public T getElemento (return element:) public void setElement(T elem) f element- elem Specifically, implement the method public static int countoccurrences (LinearNode node, T target) (Hint: use equals to compare the target with the contents of each node.)Explanation / Answer
public static int countOccurences(LinearNode node,T target)
{
int count=0;//to hold count of no of occurences
while(node!=null)//iterating till end of list
{
if(node.getElement().equals(target))//checking the data store in node with target if equals then incrementing
count++;
node=node.getNext();//getting next node
}
return count;
}