Question: I need to finish a generic LinkedList.class. Specifically, complete the maxValue and threshold methods. I CANNOT use iterators or the special for loops. I CANNOT
I need to finish a generic LinkedList.class. Specifically, complete the maxValue and threshold methods.
I CANNOT use iterators or the special for loops.
I CANNOT create new nodes.
I CANNOT use recursion.
I will rate if your code works.
For the maxValue method: Return the largest element you find in the list. If the list is empty, return null. If the largest element has duplicates, you can return any of them (for example, the first one in the list). Note that T is a generic type that extends the Comparable
For the threshold method: Threshold is an operation that given an element (called threshold), keep only those elements in the list that are smaller than the threshold, and remove all other elements. For example, if the list has 5->8->12->1->10->14->9 to begin with, and the threshold is 10, then after calling threshold method below, the list should contain only 5->8->1>9 because these are all elements that are smaller than the threshold.
Here are the codes:
class Node
class LinkedList
protected Node
public LinkedList
head = new Node
return this;
}
public void print() {
for(Node
System.out.print(node.info+" ");
}
System.out.println("");
}
public T maxValue() {
//TODO
return null;
}
public void threshold(T thres) {
//TODO
}
}
public class Test {
public static void main(String args[]) {
LinkedList
System.out.println(list.maxValue()); // should be null
list.add(20).add(30).add(10);
System.out.println(list.maxValue()); // should be 30
list.threshold(40);
list.print(); // should print out all elements
list.threshold(30);
list.print(); // should print out 10 20
list.threshold(10);
list.print(); // should print nothing
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
