In: Computer Science
This is based on LinkedLists. Please complete the methods max() and threshold(). I'd greatly appreciate it. 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. Please write a correct method in the answer so I can compare it to my wrong method
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
}
}
class Structure<T> {
public T info;
public Structure<T> link;
public Structure(T i, Structure<T> l) {
info = i; link = l;
}
}
public class LinkedList<T> {
protected Structure<T> head = null;
public LinkedList<T> add(T el) {
head = new Structure<T>(el, head);
return this;
}
public void print() {
for(Structure structure = head; structure!=null; structure=structure.link) {
System.out.print(structure.info+" ");
}
System.out.println("");
}
/*
* 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) {
return null;
}
/*
* If single node is there then node itself is highest..
*/
else {
Structure temp = head;
if (temp.link == null) {
return (T) temp.info;
}
else {
/*
* We get the value of current info
*/
T max = (T) temp.info;
/*
* This method is used get datatype and for each datatype we have to apply our
* equality condition
* In each case we compare the ax value with the current value if current value is max then
* replace them
*/
String x = max.getClass().getName();
while(temp.link!=null) {
if(x.equalsIgnoreCase("java.lang.String")) {
if (((String) temp.info).compareTo((String) max) > 0)
max = (T) temp.info;
}
else if(x.equals("java.lang.Integer")) {
if((int)temp.info>(int)max)
max=(T) temp.info;
}
else if(x.equalsIgnoreCase("java.lang.Double"))
{
if((double)temp.info>(double)max)
max=(T) temp.info;
}
else if(x.equalsIgnoreCase("java.lang.Character"))
{
if((char)temp.info>(char)max)
max=(T) temp.info;
}
temp=temp.link;
}
return max;
}
}
}
public void threshold(T thres) {//Note: Recursive methods not allowed, using new key word not allowed, no iterators allowed
if (head == null) {
return ;
}
/*
* Take two references of tye structure inititalise each from head
*First is base condition when Linked List is empty
*Secondly if first element is thres then break the entire codition(requirement not clear)
*
*/
Structure<T> temp=head;
Structure<T> ptr=head;
if(head==null)
return;
else if(head.info==thres) {
head=null;
}
/*
* In this logic I've created a while loop which continues till we found the thres or link is null
* if data is not present and we reached to last node then return
* else
* take ptr and traverse through just previous node of the temp node.
* then change the address part to remove an element.
*/
else {
while(temp.link!=null && temp.info!=thres) {
temp=temp.link;
}
if(temp.link==null && temp.info!=thres) {
return;
}
while(ptr.link!=temp) {
ptr=ptr.link;
}
ptr.link=temp.link;
}
}
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
}
}
Hope You like the solution the explaination is attached with the documentation itself. One requirement is not so cleared bu I created my logic from the example. Hope you like the solution If you do so then support us by pressing that upvote button.