Please help me with a java program that will do the following: This program will
ID: 3722288 • Letter: P
Question
Please help me with a java program that will do the following: This program will use nodes with a LinkedList the program should fill up the LinkedList with values between 25 and 1000. The program should search the LinkedList for the largest and smallest number in the list. If the minimum is greater 100 then swap it with the max value otherwise increment the min by 10 and move it one place to the next node. Also add the min and the max values and insert the new node with the calculated value in a random place between the min and max nodes.
Explanation / Answer
-----------------Code-----------------
import java.util.concurrent.ThreadLocalRandom;
class Node{
int value;
Node next = null;
}
public class LinkedList{
Node head = null;
Node lastNode = null;
public void addToEnd(int value){
Node newNode = new Node();
newNode.value = value;
if(head==null){
head = newNode;
lastNode = newNode;
}else{
lastNode.next = newNode;
lastNode = newNode;
}
}
public void swap(int i, int j){
if(i==j){return;}
if(i>j){
int temp = i;
i = j;
j = temp;
}
Node first=head,second=head;
if(i==0){
first = head;
Node curr = head;
int curri = 0;
while(curr!=null){
if(curri == j-1){
break;
}
curr = curr.next;
curri++;
}
second = curr;
Node> Node two = second.next.next;
head = second.next;
head.next = one;
second.next = first;
second.next.next = two;
}else{
Node curr = head;
int curri = 0;
while(curr!=null){
if(curri==i-1){
first = curr;
}
if(curri==j-1){
second = curr;
break;
}
curr = curr.next;
curri++;
}
Node> Node two = second.next.next;
Node temp = first.next;
if(j-i==1){
first.next = second.next;
first.next.next = temp;
temp.next = two;
}else{
first.next = second.next;
first.next.next = one;
second.next = temp;
second.next.next = two;
}
}
}
public void addRandomNumbers(int count){
for(int i=0;i<count;i++){
addToEnd(ThreadLocalRandom.current().nextInt(25, 1000 + 1));
}
}
public void minMaxSearch(){
Node minNode=head,maxNode=head;
int minI = 0, maxI = 0;
Node curr = head;
int currI = 0;
while(curr!=null){
if(curr.value<=minNode.value){
minNode = curr;
minI = currI;
}
if(curr.value>=maxNode.value){
maxNode = curr;
maxI = currI;
}
curr = curr.next;
currI++;
}
int minMax = minNode.value + maxNode.value;
if(minNode.value>100){
//Swap min and max
swap(minI,maxI);
}else{
minNode.value +=10;
//Move min to one place to next
if(minI!=currI-1){
//min is not the last node
swap(minI,minI+1);
}
}
int rInt =ThreadLocalRandom.current().nextInt(0, currI);
insertNode(rInt,minMax);
}
public void insertNode(int i, int value){
Node newNode = new Node();
newNode.value = value;
if(i==0){
newNode.next = head;
head = newNode;
}else{
int currI =0;
Node curr = head;
while(curr!=null){
if(currI==i-1){
break;
}
curr = curr.next;
currI++;
}
newNode.next = curr.next;
curr.next = newNode;
}
}
public String toString(){
String a = "";
Node curr = head;
while(curr!=null){
a += curr.value;
a += " ";
curr = curr.next;
}
return a;
}
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.addRandomNumbers(10);
System.out.println(l);
l.minMaxSearch();
System.out.println(l);
}
}