In: Computer Science
This is a Java program that I am having trouble making.
1- Search for the max value in the linked list.
2- Search for the min value in the linked list.
3- Swap the node that has the min data value with the max one. (Note: Move the nodes to the new positions).
4- Add the min value and the max value and insert the new node
with the calculated value before the last node.
I already made a generic program that creates the linked list and
has the ability to add and remove from the list. I am not sure how
to create these generic methods though.
Below is my attempt to make the min finder, but it does not work and gives me an error on the bolded parts. It says "bad operand types for binary operator >".
LList.java below:
package llist;
/**
*
* @author RH
* @param <E>
*/
public class LList<E> {
Node head, tail;
int size;
public LList() {
size = 0;
head = tail = null;
}
public void addFirst(E element) {
Node newNode = new Node(element);
newNode.next = head;
head = newNode;
size++;
if (tail == null) {
tail = head;
}
}
public void addLast(E element) {
Node newNode = new Node(element);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = tail.next;
}
size++;
}
public void insert(int index, E element) {
if (index == 0) {
addFirst(element);
} else if (index >= size) {
addLast(element);
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node holder = current.next;
current.next = new Node(element);
current.next.next = holder;
size++;
}
}
public void remove(int e) {
if (e < 0 || e >= size) {
System.out.println("Out of bounds");
} else if (e == 0) {
System.out.println("Deleted " + head.element);
head = head.next;
size--;
} else if (e == size - 1) {
Node current = head;
Node holder;
for (int i = 1; i < size; i++) {
current = current.next;
}
System.out.println("Deleted " + current.element);
tail.next = current.next;
tail = tail.next;
size--;
} else {
Node<E> previous = head;
for (int i = 1; i < e; i++) {
previous = previous.next;
}
System.out.println("Deleted " + previous.next.element);
Node<E> current = previous.next;
previous.next = current.next;
size--;
}
}
public int largestElement() {
int max = 49;
while (head != null) {
if (max < head.next) {
max = head.next;
}
head = head.next;
}
return max;
}
public int smallestElement() {
int min = 1001;
while (head != null) {
if (min > head.next) {
min = head.next;
}
head = head.next;
}
return min;
}
public void print() {
Node current = head;
for (int i = 0; i < size; i++) {
System.out.println(current.element);
current = current.next;
}
}
}
Node.java below:
package llist;
/**
*
* @author RH
* @param <E>
*/
public class Node<E> {
E element;
Node next;
public Node(E data) {
this.element = data;
}
}
Driver.java below:
package llist;
import java.util.concurrent.ThreadLocalRandom;
/**
*
* @author RH
*/
public class Driver {
public static void main(String[] args) {
LList<Integer> listy = new LList();
// adds 10 random numbers to the linkedlist
for (int i = 0; i < 10; i++) {
listy.addFirst(ThreadLocalRandom.current().nextInt(50, 1000 +
1));
}
System.out.println("");
listy.print();
System.out.println("");
int max = listy.largestElement();
int min = listy.smallestElement();
System.out.println(max);
System.out.println(min);
//listy.remove(1);
//System.out.println("");
//listy.print();
}
}
I am not sure how to get these generic methods made.
/* I HAVE CORRECTED YOUR ADDFIRST METHOD ALSO ADDED REMAINING METHODS THAT NEED TO BE IMPLEMENTED COPY AND PASTE YOUR CODE ADD YOUR PACKAGE NAME YOURSELF*/
/* Node.java */
public class Node<E> {
E element;
Node next;
public Node(E data) {
this.element = data;
this.next = null;
}
}
/*LList.java */
public class LList<E> {
Node head, tail;
int size;
public LList() {
size = 0;
head = tail = null;
}
public void addFirst(E element) {
Node newNode = new Node(element);
size++;
if(tail == null && head == null){
tail = head = newNode;
}else{
newNode.next = head;
head = newNode;
}
}
public void addLast(E element) {
Node newNode = new Node(element);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = tail.next;
}
size++;
}
public void insert(int index, E element) {
if (index == 0) {
addFirst(element);
} else if (index >= size) {
addLast(element);
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node holder = current.next;
current.next = new Node(element);
current.next.next = holder;
size++;
}
}
public void remove(int e) {
if (e < 0 || e >= size) {
System.out.println("Out of bounds");
} else if (e == 0) {
System.out.println("Deleted " + head.element);
head = head.next;
size--;
} else if (e == size - 1) {
Node current = head;
Node holder;
for (int i = 1; i < size; i++) {
current = current.next;
}
System.out.println("Deleted " + current.element);
tail.next = current.next;
tail = tail.next;
size--;
} else {
Node<E> previous = head;
for (int i = 1; i < e; i++) {
previous = previous.next;
}
System.out.println("Deleted " + previous.next.element);
Node<E> current = previous.next;
previous.next = current.next;
size--;
}
}
public int largestElement() {
int max = (int)head.element ;
Node curr = head;
curr = curr.next;
while (curr != null) {
if (max < (int)curr.element) {
max = (int)curr.element;
}
curr = curr.next;
}
return max;
}
public int smallestElement() {
int min = (int)head.element;
Node curr = head;
curr = curr.next;
while (curr != null) {
if (min > (int)curr.element) {
min = (int)curr.element;
}
curr = curr.next;
}
return min;
}
public void print() {
Node current = head;
for (int i = 0; i < size; i++) {
System.out.println(current.element);
current = current.next;
}
}
public void swapNodes(int x, int y)
{
// Nothing to do if x and y are same
if (x == y) return;
// Search for x (keep track of prevX and CurrX)
Node prevX = null, currX = head;
while (currX != null && (int)currX.element != x)
{
prevX = currX;
currX = currX.next;
}
// Search for y (keep track of prevY and currY)
Node prevY = null, currY = head;
while (currY != null && (int)currY.element != y)
{
prevY = currY;
currY = currY.next;
}
// If either x or y is not present, nothing to do
if (currX == null || currY == null)
return;
// If x is not head of linked list
if (prevX != null)
prevX.next = currY;
else //make y the new head
head = currY;
// If y is not head of linked list
if (prevY != null)
prevY.next = currX;
else // make x the new head
head = currX;
// Swap next pointers
Node temp = currX.next;
currX.next = currY.next;
currY.next = temp;
}
}
/* Driver.java */
import java.util.concurrent.ThreadLocalRandom;
public class Driver {
public static void main(String[] args) {
LList<Integer> listy = new LList();
// adds 10 random numbers to the linkedlist
for (int i = 0; i < 10; i++) {
listy.addFirst(ThreadLocalRandom.current().nextInt(50, 1000 +
1));
}
System.out.println("");
listy.print();
System.out.println("");
int max = listy.largestElement();
int min = listy.smallestElement();
System.out.println("1. Max: "+max);
System.out.println("2. Min: "+min);
System.out.println("3. Swap Min and Max ");
listy.swapNodes(max, min);
System.out.println("");
listy.print();
int sum = min + max;
System.out.println("4. Add sum of min and max at second
last");
int index = listy.size-1;
listy.insert(index, sum);
System.out.println("");
listy.print();
System.out.println("");
listy.remove(1);
System.out.println("");
listy.print();
}
}
/* OUTPUT */