Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

lab2.java : import java.util.Random; public class lab2 { public static void main(String[] args){ LinkedList ll = createLinkedList(); //create a link list and top will point

image text in transcribed

lab2.java :

import java.util.Random;

public class lab2

{

public static void main(String[] args){

LinkedList ll = createLinkedList(); //create a link list and top will 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 with random values

//you can modified this routine to generate other kinds of test data

public static LinkedList createLinkedList() {

//use random number generator to generate a random list of numbers

LinkedList ll = new LinkedList();

Random rn = new Random();

int n = rn.nextInt(100)%11; /umber of inputs between 0 and 10.

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 of list

*

* PARAM INPUT top: a reference pointing at the first node in the list

*

* RETURN a reference pointing at the first node in the new list

***/

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 the list

***/

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 no such node exists)

public Node(int item, Node next){

this.item = item;

this.next = next;

}

public Node(int item){

this.item = item;

this.next = null;

}

}

Exercise Download the java code lab2.java from UMLearn. Fill in the code for the method called maxToFront The input parameter to this method is a reference pointing at the first node in a linked list. This method should move the smallest element value in the linked list to the front of the list, then return the reference pointing at the front of the (new) list Suppose we have the following linked list 9 12 null Then the method maxToFront will move the node containing 17 to the front of the list and return a reference pointing to the first node of the linked list, as given below 17 9 12 null If the list has more than one occurrence of the maximum value, only the first occurrence should be moved. For example, becomes 17 null If the list is empty, the method should have no effect. Your method must work under all cir cumstances, including empty lists and lists with only one (data) node and lists where the maximum value occurs multiple times Do not create any new Node object (though you may use as many Node variables as you like) Also do not change the value of item of any existing node. You should solve the task traversing the linked list and manipulating the links between the nodes

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

Students also viewed these Databases questions

Question

Wear as little as possible

Answered: 1 week ago

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago