This is based on LinkedLists. There is a type mismatch in my first method and I dont know how to get around it. I've commented on the line with the type mismatch
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, using
new 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 not
allowed, 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
}
}
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, using new 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
//In your case you actually passing address of the next node in the max.
//Instead you have to find pass the info part of linked list not the address.
T max=temp.info;
if (temp.info.compareTo(max) > 0)
max = temp.info;
return max;
}
}
}
public void threshold(T thres) {//Note: Recursive methods not allowed, 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
}
}
In you case you are trying to access the memory location but you have to pass the value to the max value not the address..Explaination and correct line is given inside the code near the error occurs.
I hope you like the solution...if you do so then support us by pressing that upvote button.
Get Answers For Free
Most questions answered within 1 hours.