Fill in the code for the method called maxToFront. The inputparameter to this method is a reference

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Order a Similar Paper Order a Different Paper

Fill in the code for the method called maxToFront. The inputparameter to this method is a reference pointing at the first nodein a linked list. This method should move the node with largestdata value in the linked list to the front of the list, then returnthe reference pointing at the front of the (new) list. Suppose wehave the following linked list: 7->9->12->5->17->3->null Then the method maxToFront will move the node containing 17 tothe front of the list and return a reference pointing to the firstnode of the linked list, as given below. 17->7->9->12->5->3->null If the list has more than one occurrence of the maximum value,only the first occurrence should be moved. For example, 7->9->17->5->17->3->null becomes 17->7->9->5->17->3->null If the list is empty, the method should have no effect. Yourmethod must work under all circumstances, including empty lists andlists with only one (data) node and lists where the maximum valueoccurs multiple times. Do not create any new Node object (thoughyou may use as many Node variables as you like). Also do not changethe value of item of any existing node. You should solve the tasktraversing the linked list and manipulating the links between thenodes. import java.util.Random;
public class lab2
{
public static void main(String[] args){
  
LinkedList ll = createLinkedList(); //create a link list and topwill point to the start of the linked list
  
// print the items in the original linked list
System.out.println(“Original List:”);
ll.printList();
// move the minimum element to the front of list
System.out.println(“Altered List:”);
ll.maxToFront(); // print the items in the new linked list
ll.printList();
} //this method creates an unordered linked list filled withrandom values
//you can modified this routine to generate other kinds of testdata
public static LinkedList createLinkedList() {
//use random number generator to generate a random list ofnumbers
  
LinkedList ll = new LinkedList();
Random rn = new Random();
int n = rn.nextInt(100)%11; //number of inputs between 0 and10.
  
int[] a = new int[n];
int num;
Node top = null;
  
for (int i = 0;i
num = rn.nextInt(Integer.MAX_VALUE)% 10;
ll.insert(num);
}
  
return ll;
}
} class LinkedList{ private Node top; //reference to first node in list /***
*LinkedList – initialize list to empty list
***/
public LinkedList() {
top = null;
} /***
* maxToFront – Move the node with the minimum value to the front oflist
*
* PARAM INPUT top: a reference pointing at the first node in thelist
*
* RETURN a reference pointing at the first node in the newlist
***/
  
public void maxToFront(){ // fill in your code here
  
} /***
* insert – insert a new node to front of list
*
* PARAM INPUT i: data for the new node to be inserted
***/
public void insert(int i) {
Node newNode = new Node(i,top);
top = newNode;
} /***
* printList – Print the items in a linked list
*
* PARAM INPUT top: a reference pointing at the first node in thelist
***/ public void printList(){
Node curr = top;
while (curr != null){
System.out.print(curr.item + ” “);
curr= curr.next;
}
System.out.println();
} } //A node in a singly-linked linked list
class Node
{
public int item; //data to be stored
public Node next; //reference to the next node (set to null if nosuch node exists) public Node(int item, Node next){
this.item = item;
this.next = next;
} public Node(int item){
this.item = item;
this.next = null;
}
} Attached