Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I got this code i just need help figuring out how to do the missing methods recursively for the linked list. public class ListOperations {

I got this code i just need help figuring out how to do the missing methods recursively for the linked list.

public class ListOperations {

public static void main(String[] args){

MyListOfInts t=null;

for (int i=0; i<5;i++){

//int ran = (int) (100.0* Math.random());

int ran = i+2;

t=new MyListOfInts(ran, t);

}

System.out.println("All numbers in the list:");

printMyList(t);

System.out.println();

System.out.println("Sum="+sumOfMyList(t));

System.out.println("Max="+maxOfMyList(t));

System.out.println("Length="+lengthOfMyList(t));

t=reverseMyList(t);

System.out.println("All numbers in the reversed list:");

printMyList(t);

System.out.println();

System.out.println("Remove 3");

MyListOfInts tt=removeANumberFromMyList(t, 3);

System.out.println("All numbers in the new list:");

printMyList(tt);

System.out.println();

System.out.println("All numbers in the previous list:");

printMyList(t);

System.out.println();

System.out.println(

"Insert a number in a position of the new list:");

tt=insertANumberIntoMyList(tt, 20, 1);

printMyList(tt);

System.out.println();

System.out.println(

"Values obtained for the special recursive method:");

reportOnValues(tt);

}

/* Write a recursive method to print all the numbers separated by spaces.

This method cannot contain any loop (that is, uses of for, while, do

while are prohibited).

*/

public static void printMyList(MyListOfInts m){

/* Write your code here */

if(m == null) { //base case

return;

}

System.out.println(m.firstInt); //print out first int

printMyList(m.restOfTheInts); //keep calling the method (printing the first int)

}

/* Write a recursive method to retrieve the sum of all the numbers in a list.

This method cannot contain any loop (that is, uses of for, while, do while

are prohibited).

*/

public static int sumOfMyList (MyListOfInts m){

/* Write your code here */

if(m == null) {

return 0; //base case

}

int sumR = sumOfMyList(m.restOfTheInts); //set sumR (rest of the ints) to the sum of the rest of the integers

return m.firstInt + sumR; //add the rest of the ints to the first integer

}

/* Write a recursive method to retrieve the largest number from the list.

Assume that there is at least one number in the given list when the method

is called from the main function. This method cannot contain any loop (that

is, uses of for, while, do while are prohibited).

*/

public static int maxOfMyList (MyListOfInts m){

if(m == null) {

return Integer.MIN_VALUE; //base case

}

int MaxL = maxOfMyList(m.restOfTheInts); //assign maxL to the rest of the ints

if(m.firstInt > MaxL) {

return m.firstInt; //check if the first int is bigger than the rest as it iterates and return the largest one

}

return MaxL; //return maxL if it is bigger than the first int

/* Write your code here */

}

/* Write a recursive method to compute the length of a list.

This method cannot contain any loop (that is, uses of for, while, do while

are prohibited).

*/

public static int lengthOfMyList (MyListOfInts m){

/* Write your code here */

if(m.restOfTheInts == null) {

return 1; //base case

}

return 1 + lengthOfMyList(m.restOfTheInts); // create a counter to increase every time there is a integer

}

/* Write a recursive method named reverseMyList that will reverse a given

linked list m. Return the head of the reversed linked list. It is fine

if you need to modify the given linked list to obtain the reversed one.

The method reverseMyList may not contain any loop.

*/

public static MyListOfInts reverseMyList (MyListOfInts m){

/* Write your code here */

MyListOfInts temp = m;//create copy of the head

if (temp == null){//if the current node is == to null then return null, this is checking for empty list

return null;

}

if (temp.restOfTheInts == null){//if the next is node is null then return the current node

return temp;

}

MyListOfInts secondElem = temp.restOfTheInts;//create a second node and assign the next node to it

temp.restOfTheInts = null;//next node is now null

MyListOfInts reverseHead = reverseMyList(secondElem);//reversed list with the second element node value

secondElem.restOfTheInts = temp;//secondElm.restOfTheInts is now pointing to temp

return reverseHead;//return the reverseHead

}

/* Write a recursive method to remove the first occurrence of a specific

number from a list. As an example, if your list initially contains

20 5 10 3 5 1 and your removee is 5, the returned list should contain

20 10 3 5 1 after the removal. Return a new head. You must make sure that

the parameter list remains intact after returning the new list to the main

method. This method cannot contain any loop.

*/

public static MyListOfInts removeANumberFromMyList(MyListOfInts m, int removee){

if(m == null) throw new RuntimeException("removee not found ");

if(m.firstInt == removee) return m.restOfTheInts;

return new MyListOfInts(m.firstInt, removeANumberFromMyList(m.restOfTheInts, removee));

}

/* Write a recursive method to insert a number (insertee) into a specific

position of a list. Positions start from 0 (that is, the position of

the head of a list is 0). This method cannot contain any loop (that is,

uses of for, while, do-while are prohibited).

*/

public static MyListOfInts insertANumberIntoMyList(MyListOfInts m, int insertee, int position){

/* Write your code here */

}

/* Write a recursive method named reportOnValues that will use each value in

a list to compute a recursive formula implemented in the method

specialRecursiveFunction. This method cannot contain any loop.

*/

public static void reportOnValues(MyListOfInts m){

/* Write your code here. This method must call specialRecursiveFunction

*/

}

/*This method cannot contain any loop */

public static double specialRecursiveFunction(int x){

/* Write your code here */

}

}

public class MyListOfInts {

public int firstInt;

public MyListOfInts restOfTheInts;

public MyListOfInts(int f){

firstInt=f;

}

public MyListOfInts(int f, MyListOfInts r){

firstInt=f;

restOfTheInts=r;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions