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.
Implement a function that returns the maximum number in a given unsorted linked list. For example,...
Implement a function that returns the maximum number in a given unsorted linked list. For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example. 1. Implement max.c with the completed printMax() function. 2. Provide an explanation for your solution #include <stdio.h> typedef struct node { int value; struct node *next; } node; int printMax(node *first) { // Your implementation return 0; } int...
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
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
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT