Question

In: Computer Science

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

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

Solutions

Expert Solution


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.


Related Solutions

Hello! Could someone please answer this for me? I would greatly appreciate it, and will totally...
Hello! Could someone please answer this for me? I would greatly appreciate it, and will totally like your answer! Length isn't necessarily important, just needs to be accurate and such. Thanks in advance!!!!!!! (References are needed when using examples, or include the link from where you got it and I can format the reference). "Discuss the difference in the cost of items sold by a retail shoe store, the cost of items sold by a shoe manufacturer, and the cost...
Can someone please properly answer the 3 questions listed below? I'd really appreciate it. And also,...
Can someone please properly answer the 3 questions listed below? I'd really appreciate it. And also, please (TYPE) everything out. 27- Considering a central bank for which the keeping the price level stable is much more important than stabilizing the output level. Draw the AD curve associated with this central bank. 28- Explain the binding situation and draw the Fed Rule and AD curve associated with binding situation. 29- What is stagflation?
If anyone can "dumb this down for me I'd appreciate it. I'm really not good with...
If anyone can "dumb this down for me I'd appreciate it. I'm really not good with statistics, I'm easily confused by all the terms and symbols so if someone is willing to explain a basic easy to understand example that would be much appreciated- What types of misconceptions and errors do your foresee occurring in a business setting if the normal distribution assumptions are not applied correctly? Please include a real-world business example in your response.
I'm having a pretty difficult time with these types of problems and I'd really appreciate it...
I'm having a pretty difficult time with these types of problems and I'd really appreciate it if someone could show me how to go about doing this one, thank you! 1. Consider the population of {32, 34, 37, 39} a) Find the mean, variance, and standard deviation b) Suppose the sample size n=2 is randomly chosen with replacement from this population, List the 16 possible samples of size n=2 c) Fill out the table Sample Size (n=2) Sample Mean Sample...
If you could explain this and answer it, I would greatly appreciate it. C. Use Excel...
If you could explain this and answer it, I would greatly appreciate it. C. Use Excel (or equivalent application) to determine how long it will take for an investment to triple in value at interest rates of 1%, 5%, 10%, 15%, 20%, and 25%. Can you determine an approximate “Rule” for how to quickly calculate how long it takes for an investment to triple in value?
How can someone have a high vo2 max but a low lactate threshold?
How can someone have a high vo2 max but a low lactate threshold?
Hi. I am running into trouble solving the problem below and would greatly appreciate a step-by-step...
Hi. I am running into trouble solving the problem below and would greatly appreciate a step-by-step explanation on how to arrive at the correct answers and explanations: SHIELD, Inc., uses a standard costing system and develops its overhead rates from the current annual budget. The budget is based on an expected annual output of 120,000 units requiring 480,000 direct labor hours. (Practical capacity is 500,000 hours.) Annual budgeted overhead costs total $787,200, of which $556,800 is fixed overhead. A total...
Hello. I am running into difficulty solving the problem below and would greatly appreciate a step-by-step...
Hello. I am running into difficulty solving the problem below and would greatly appreciate a step-by-step explanation on how to arrive at the correct answer: NATLA Company manufactures consumer products and provided the following information for the month of February: Units produced 131,000 Standard direct labor hours per unit 0.20 Standard variable overhead rate (per direct labor hour) $3.40 Actual variable overhead costs $88,670 Actual hours worked 26,350 1. Calculate the variable overhead spending variance and variable overhead efficiency variance...
The text doesn't offer very much specific information about this topic, and i would greatly appreciate...
The text doesn't offer very much specific information about this topic, and i would greatly appreciate help! question- what type of changes/shifts/trends do we see with aggression and antisocial behaviors during adolescence?
Based on purchasing power parity theory, should the Mexican Peso appreciate or depreciate? Please provide ample...
Based on purchasing power parity theory, should the Mexican Peso appreciate or depreciate? Please provide ample justification
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT