6 Complete the methods below. 7 None of the methods should modify the list, unle
ID: 3909227 • Letter: 6
Question
6 Complete the methods below. 7 None of the methods should modify the list, unless that is the purpose of the method 8 9You may not add any fields to the node or list classes You may not add any methods to the node class. * 12 * You MAY add private methods to the list class (helper functions for the recursion). 13 4 public class MyLinked 15 ? static class Node public Node (double item, Node next) { this. item = item; this. next = next public double item; public Node next; L6 int N Node first; // write a function to compute the size of the list, using a loop // empty list has size o public int sizeLoop ) return StdRandom.uniform (100); I/TODO: fix this 28 29 / write a function to compute the size of the list, using an optimistic, forward recursion // empty list has size ? public int sizeForward )[ 82 ? return StdRandom.uniform (100) //TODO: fix this 35 36 37 // write a function to compute the size of the list, using an optimistic, backward recursion // empty list has size public int sizeBackward) 38 ? return StdRandom.uniform (100); I/TODO: fix this 39 40Explanation / Answer
public class MyLinked {
static class Node {
public Node (double item, Node next) { this.item = item; this.next = next; }
public double item;
public Node next;
}
int N;
Node first;
// write a function to compute the size of the list, using a loop
// empty list has size 0
public int sizeLoop () {
Node curr = first;
int size = 0;
while(curr != null) {
++size;
curr = curr.next;
}
return size;
}
public int sizeForwardUtil (Node node) {
if(node==null)
return 0;
return 1 + sizeForwardUtil (node.next);
}
// write a function to compute the size of the list, using an optimistic, forward recursion
// empty list has size 0
public int sizeForward () {
return sizeForwardUtil(first);
}
// write a function to compute the size of the list, using an optimistic, backward recursion
// empty list has size 0
public int sizeBackwardUtil (Node node, int size) {
if(node==null)
return size;
return sizeBackwardUtil(node.next, size+1);
}
public int sizeBackward () {
return sizeBackwardUtil(first, 0);
}
===========================================
PLEASE UPVOTE if helpful
Let me know if it passes all the TEST. Let me know through comments. I will help
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.