Question: 3 . Implement a list with recursive methods: The class Q 3 _ RecList implements a list. We will implement the methods: printList , find

3. Implement a list with recursive methods:
The class Q3_RecList implements a list. We will implement the methods: printList,find 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 Q3_RecList {
Link head;
public void append(int e){
if (head == null){
head = new Link(e);
}
else {
recAppend(head, e);
}
}
/**
* Helper function for append
*/
private void recAppend(Link curr, int e){
if (curr.next == null){
curr.next = new Link(e);
}
else {
recAppend(curr.next, e);
}
}
/**
* Prints the list in format [ a0 a1 a2 a3 a4]
* Where a0 is the integer at index 0, a1 is the integer at index 1 etc.
*/
public void printList(){
System.out.print("[");
System.out.print(recPrint(head));
System.out.println("]");
}
/**
* Helper function for printList
*/
private String recPrint(Link curr){
return ""; //STUB
}
/*
* Returns the index (0 based) of the first occurrence of e, if not found, return the size of the list
* Example: find 4 in [2,5,4,3,4] returns 2
* find 8 in [2,5,4,3,4] returns 5
* find 4 in [] returns 0
*/
public int find(int e){
if (head == null){
return 0;
}
else {
return 0; // STUB
}
}
/**
* Helper function for find
*/
private int recFind(Link curr, int e){
return 0; // STUB
}
/**
* Returns the sum of elements recursively
* You cannot change this signature, however you can use a helper recursive function
*/
public int sumOfList(){
return 0; // STUB
}
// inner class link
class Link{
int val =0;
Link next = null;
Link(int n){
val = n;
}
Link(int 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!