Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a code in java. Write two functions to find the the number ofelements in a

ID: 3612746 • Letter: W

Question

write a code in java. Write two functions to find the the number ofelements in a linked list: one using a loop, and one usingrecursion. To represent the linked list use the class lp: publicclass lp { public int first; public lp rest; public lp(int first1,lp rest1) { first = first1; rest = rest1; } } The version with theloop looks like the function I showed you in class, which printsall the elements. All you have to do is add a counter, andincrement it in the body of the loop. When you reach the end, thecounter's value is the length. The recursive version looks like therecursive function for displaying elements of a list. To count theelements, you must rewrite the function so it returns an integer --the length. In the base case return a zero. In the recursive case,the recursive call returns an integer -- your answer is one morethan that integer. Your program should build a list of length 3 andthen call the two length functions to find its length. Building thelist is easy: list1 = new lp(1, new lp(2, new lp(3, null))); Yourprogram doesn't need any input. It should look like this to theuser: length computed with a loop: 3 length computed withrecursion: 3 </pre> write a code in java. Write two functions to find the the number ofelements in a linked list: one using a loop, and one usingrecursion. To represent the linked list use the class lp: publicclass lp { public int first; public lp rest; public lp(int first1,lp rest1) { first = first1; rest = rest1; } } The version with theloop looks like the function I showed you in class, which printsall the elements. All you have to do is add a counter, andincrement it in the body of the loop. When you reach the end, thecounter's value is the length. The recursive version looks like therecursive function for displaying elements of a list. To count theelements, you must rewrite the function so it returns an integer --the length. In the base case return a zero. In the recursive case,the recursive call returns an integer -- your answer is one morethan that integer. Your program should build a list of length 3 andthen call the two length functions to find its length. Building thelist is easy: list1 = new lp(1, new lp(2, new lp(3, null))); Yourprogram doesn't need any input. It should look like this to theuser: length computed with a loop: 3 length computed withrecursion: 3 </pre>

Explanation / Answer

I will assume the existence of a getRest() method, which returns thevariable Ip.rest according to convention. If the codeis is in the Ip class, or if Ip.restcan otherwise be accessed, use that instead of getRest(). public int getSize(Ip root) {    // initialize count    // initialize count    int count=0;    // check if root is null    // check if root is null    while(root!=null)    {       // if root is not null, increment counter& check next       // if root is not null, increment counter& check next       count++;       root=root.getRest();    }    // if root is null, return counter    // if root is null, return counter    returncount; } recursive: public int getSize2(Ip root) {    // check if root is null    // check if root is null    if(root==null)    {    // if root is null, return counter    // if root is null, return counter       return0;    }    // if root is not null, increment counter and check nextnode    // if root is not null, increment counter and check nextnode    else    {       return1+getSize2(root.getRest());    } } // main()method public static void main(String[]args) {    list1 = new lp(1, newlp(2, new lp(3, null)));    out.println("lengthcomputed with a loop: "+getSize(list1));    out.println("lengthcomputed with recursion: "+getSize2(list1)); } Both programs do the same thing: check the node to see whether it's null if it is null, return counter if it is not null, increment counter, go to next node andrepeat. Both programs do the same thing: check the node to see whether it's null if it is null, return counter if it is not null, increment counter, go to next node andrepeat.