This is based on LinkedLists. Please complete the methods max() and threshold(). I'd greatly appreciate it....

90.2K

Verified Solution

Question

Programming

This is based on LinkedLists. Please complete the methods max()and threshold(). I'd greatly appreciate it. There is a typemismatch in my first method and I dont know how to get around it.I've commented on the line with the type mismatch. Please write acorrect method in the answer so I can compare it to my wrongmethod

public class Node {
   public T info;
public Node link;
public Node(T i, Node l) {
   info = i; link = l;
   }
}

class LinkedList> {
  
protected Node head = null;
public LinkedList add(T el) {
head = new Node(el, 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() { // Note: Recursive methods not allowed, usingnew key word not allowed, no iterators allowed
   if (head == null) {
   return null;
   }
   else {
   Node temp = head;
   if (temp.link == null) {
   return temp.info;
   }
   else {
   T max = temp.link; //type mismatch
   if (temp.info.compareTo(max) > 0)
   max = temp.info;
   return max;
   }
}
}
  
public void threshold(T thres) {//Note: Recursive methods notallowed, using new key word not allowed, no iterators allowed

}

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
}
}

Answer & Explanation Solved by verified expert
4.1 Ratings (653 Votes)
class Structure public T info public Structure link public StructureT i Structure l info i link l public class LinkedList protected Structure head null public LinkedList addT el head new Structureel head return this public void print forStructure structure head structurenull structurestructurelink Systemoutprintstructureinfo Systemoutprintln It is your maxValue function public T maxValue Note Recursive methods not allowed using new key word not allowed no iterators allowed Base critera to return when Linked list is empty if head null    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students