Question 4 (10 marks) Consider the class SinglyLinkedList outlined below public
ID: 3707929 • Letter: Q
Question
Question 4 (10 marks) Consider the class SinglyLinkedList outlined below public class SinglyLinkedList implements List t private static class Node I private Boolean value; private Node next; private NodeBolean value, Node next ( this value value; this next next; private Node head public boolean sEmpty ) return head = null; /I class continues after that We want to add the method andO to the class SinglyLinkedList. The method andO is a recursive method that returns true if and only if all the elements of the list instance are true. The bchaviour of the method is undefined if the list is empty For example, the code below prints true then false SinglyLinkedList test new SinglyLinkedList ); test.add (true) test add (true); test add (true); System. out. println (test and )): I prints "rue" test add (false): test add (true); System. out. println (test and )): I prints "false" Provide the recursive implementation of the method in the following box. Warning: make sure that your implementation is as efficient as possible. Two marks will be removed for an inefficient implementation!Explanation / Answer
Recursive "AND" Method
public boolean andHelper(Node curr) {
if(curr==null)
return true;
if(!curr.value)
return false;
return andHelper(curr.next);
}
public boolean and() {
if(head==null)
return true;
return andHelper(head);
}
Thanks, let me know if there is any concern. PLEASE UPVOTE if its helpful
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.