Question

In: Computer Science

write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....

write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0.

use the provided code below

public class Question03 
{
        public class ListNode//public for testing purposes
        {
                public int data;//public for testing purposes
                public ListNode link;//public for testing purposes
                public ListNode(int aData, ListNode aLink)
                {
                        data = aData;
                        link = aLink;
                }
        }
        public ListNode head;//public for testing purposes
        public int getMaxValue()
        {
//-----------------------------------------------------------------------------------
                //Write your solution here

                
                
                
                
                
                
                
                
                
                
                
                
                
                

        }//Do not alter this
        //Write additional methods or properties here
                        
        //--------------------------------------------------------------------------------
        //Test your code here. You may alter this as needed.
        public static void main(String[] args)
        {
                //Example
                Question03 intLL = new Question03();
                intLL.head = intLL.new ListNode(0, 
                                                        intLL.new ListNode(1, 
                                                                        intLL.new ListNode(2,
                                                                                        intLL.new ListNode(3,
                                                                                                        intLL.new ListNode(4,null)))));
                int max = intLL.getMaxValue();
                System.out.println(max);
        }
        //--------------------------------------------------------------------------------
}//Do not alter this

Solutions

Expert Solution

public ListNode head;//public for testing purposes,public ListNode head=null;
        public int getMaxValue()
        {

               

            ListNode temp=head;   //head assigned to temp,not to alter the haead
             int max;
            if(head==null)       //if no elements, return 0
            {//System.out.println("no elements");
                 return 0;
            }
            else
            {
                max=head.data; //head data is assigned to max
            
                while(temp!=null)   // traverse the list
                {
                // System.out.println(temp.data);  
             
                    if(max<temp.data)     //if temp.data > max, the assign it as max
                    {
                        max=temp.data;
                    }
                    temp=temp.link;  //temp points to next node
            
                }
            }
            return max;      

        }//Do not alter this

Related Solutions

write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write a function maxFun() that returns the maximum value in any given integer array. Determine the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the function parameters (complete the area shown in ___________. ___________maxFun(____________________________) { }
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question 2: Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1. Please use C++ language for both questions, I only need functions.
Create a ValueGet() method that takes a linked list as input and an integer index and...
Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position. Sample Input: 01->100->300->214, index = 2 Output: 300 At index 2 the node has a value of 300 give me the full code. Give the full code with c++
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT