Please read the directions and do this assignment correctly. I need the filename
ID: 3539491 • Letter: P
Question
Please read the directions and do this assignment correctly. I need the filename to be LLMethods2, pass the two drivers below, and I need your code to simply compile. Also explain your code with comments withing the code. Thank You!
For this homework, you will write two methods, each method will go into the
LLMethods2.java file. Be sure to include the following file in the same directory as LLMethods2:
The first method you will write will have the following header
This method will recursively insert a thing (value) into a linked list (list) in order. Be sure to consider the case where list is empty.
The psuedocode for this method is:
.
The second method has the following header:
This method will take a linked list (list) in which the values are in any order and return a list in which the elements are in sorted order.
The psuedocode for this method is:
. .
Explanation / Answer
//POSTED PROPER CODE
//USES THE LinkedList class ( the file included in the assignment)
public class LLMethods2
{
//INSERT
public static LinkedList insert(LinkedList list, int value)
{
// base case, if list is empty.
if(list==null)
{
//create new linkedlist value
list=new LinkedList();
list.value=value;
list.next=null;
//return just that value
return list;
}
else if( value<=list.value)
{
//create new linkedlist value
LinkedList newVal=new LinkedList();
newVal.value=value;
newVal.next=list;
//insert value at this position in the list
list=newVal;
return list;
}
else
{
//insert value into the next linked list.
list.next=insert(list.next,value);
return list;
}
}
//SORT
public static LinkedList sort(LinkedList list)
{
//base case
if(list==null)
{
//if list is empty,then list is already sorted.
return list;
}
else
{
//recursive case -
//return the result of inserting list.value into the result of sort(list.next)
return insert(sort(list.next),list.value);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.