Question: 3 . Implement a list with recursive methods: The class Q 3 _ RecList implements a list. We will implement the methods: printList , find
Implement a list with recursive methods:
The class QRecList implements a list. We will implement the methods: printListfind and sumOfList
The implementation is via a linked list, and all operations must be done recursively, so NO LOOPS.
The method append is implemented as a reference.
public class QRecList
Link head;
public void appendint e
if head null
head new Linke;
else
recAppendhead e;
Helper function for append
private void recAppendLink curr, int e
if currnext null
curr.next new Linke;
else
recAppendcurrnext, e;
Prints the list in format a a a a a
Where a is the integer at index a is the integer at index etc.
public void printList
System.out.print;
System.out.printrecPrinthead;
System.out.println;
Helper function for printList
private String recPrintLink curr
return ; STUB
Returns the index based of the first occurrence of e if not found, return the size of the list
Example: find in returns
find in returns
find in returns
public int findint e
if head null
return ;
else
return ; STUB
Helper function for find
private int recFindLink curr, int e
return ; STUB
Returns the sum of elements recursively
You cannot change this signature, however you can use a helper recursive function
public int sumOfList
return ; STUB
inner class link
class Link
int val ;
Link next null;
Linkint n
val n;
Linkint n Link next
val n;
this.next next;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
