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 interface (so you should use its compareTo method to compare two elements of this type). NO more than 12 lines of code.

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 { public T info; public Node link; public Node(T in, Node li) { info = in; link = li; } }

class LinkedList> {

protected Node head = null;

public LinkedList add(T elem) {

head = new Node(elem, head);

return this;

}

public void print() {

for(Node node = head; node!=null; node=node.link) {

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 list = new 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!