Write a method in the class LinkedListRec <EE> class named search. This method r
ID: 3598540 • Letter: W
Question
Write a method in the class LinkedListRec <EE> class named search. This method returns true if its argument is stored as the data field of a LinkedListRec node. The method returns false if its argument is not stored in any LinkedListRec node.
NOTE: You MUST use recursion in your implementation...It means that you MUST write the following public wrapper method and another method that is the private recursive counterpart of this public wrapper.
HINT : check how the recursive linear search is done..
public boolean search ( E target )
Explanation / Answer
Hi friend you have not posted the structure of class LinkedListRec <EE> class. Also you have not posted structure of Node class
I am assuming structure of Node class i:
class Node<E> {
E data;
Ndoe next;
}
Ans I am assuming that there a variable "head" of type Node of class LinkedListRec <EE> :
class LinkedListRec<E> {
Node head;
......
}
public boolean search ( E target ) {
return searchRec(Node<E> head, E target) {
}
}
private boolean searchRec(Node<E> head, E target) {
if(head == null)
return false;
if(target.compareTo(head.data) == 0)
return true;
return searchRec(head.next, target);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.