*You may not add any fields to the node or list classes *You may not add any met
ID: 3594787 • Letter: #
Question
*You may not add any fields to the node or list classes *You may not add any methods to the node class. * You MAY add private methods to the class (helper functions for the recursion) static boolean showMeSuccess-false; I/ set to true to also see Success notifications for tests // set to false to only see Failure notifications for tests static class Node [ public Node (double item , Node next) { thi s .item- item; this.next = next; } public double item; public Node next Node first; // a function to compute the size of the list, using a loop // an empty list has size public int sizeIterative O return StdRandom.uniform (100) //TODO 1: fix this // a function to compute the size of the list using recursion // empty list has size // You will want to create a helper function to do the recursion public int sizeRecursive O return StdRandom.uniform (100) I/TODO 2: fix thisExplanation / Answer
public int sizeIterative()
{
if (first == null)
return 0;
int size = 0;
Node curr = first;
while (curr!=null ){
++size;
curr = curr.next;
}
return size;
}
====================================================
public int sizeRecusiveUtil(Node head){
if (head == null)
return 0;
return 1 + sizeRecusiveUtil(head.next);
}
public int sizeRecursive()
{
return sizeRecusiveUtil(first);
}
===============================================================
Thanks, let me know if there is any concern. Recurive uses helper method, so Keep both
Iterative calculates itself
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.