Question

In: Computer Science

Find the largest and smallest element of a linked list, print total of all elements and...

Find the largest and smallest element of a linked list, print total of all elements and find out the average.

Code needed in java

Solutions

Expert Solution

We can use java.util.LinkedList class for creating a linkedList and implement the program as follows:

  1. import LinkedList class from java.util
  2. define the class LLOperations
  3. define static method findMin() that accepts linkedlist object
  4. check if the size of list is greater than 0
  5. initialize min as the first element in list
  6. for each element in list
  7. check if min > any of the element in list
  8. if yes, update min as the current element
  9. return min
  10. return -1 if the size = 0
  11. define static method findMax() that accepts linkedlist object
  12. check if the size of list is greater than 0
  13. initialize max as the first element in list
  14. for each element in list
  15. check if max < any of the element in list
  16. if yes, update max as the current element
  17. return max
  18. return -1 if the size = 0
  19. define static method findTotal() that accepts linkedlist object
  20. initialize total as 0
  21. for each element in list
  22. add each element to total
  23. return total
  24. define static method findAvg() that accepts linkedlist object
  25. initialize avg as 0.0
  26. check if the size of list is greater than 0
  27. calculate average as, average = total/size
  28. return average, avg
  29. define main() method
  30. create and initialize a linkedlist
  31. add elements to linkedlist
  32. print all elements in linkedlist
  33. print smallest element in linkedlist using findMin()
  34. print largest element in linkedlist using findMax()
  35. print total using findTotal()
  36. print average using findAvg()

Program: LLOperations.java

import java.util.LinkedList;                                                                        /* import LinkedList class from java.util */

public class LLOperations {                                                                     /* define the class LLOperations */
        
        public static int findMin(LinkedList<Integer> Llist){             /* define static method findMin()that accepts linkedlist object */
                if(Llist.size()>0){                                                                  /* check if the size of list is greater than 0 */
                        int min = Llist.get(0);                                                         /* initialize min as the first element in list */
                        for(int i=0; i<Llist.size(); i++){                                   /* for each element in list */
                                if(Llist.get(i)<min){                                                        /* check if min > any of the element in list */
                                        min = Llist.get(i);                                             /* if yes, update min as the current element */
                                }
                        }
                        return min;                                                                             /* return min */
                }
                else
                        return -1;                                                                                      /* return -1 if the size = 0 */
        }
        
        public static int findMax(LinkedList<Integer> Llist){             /* define static method findMax()that accepts linkedlist object */
                if(Llist.size()>0){                                                                          /* check if the size of list is greater than 0 */
                        int max = Llist.get(0);                                                         /* initialize max as the first element in list */
                        for(int i=0; i<Llist.size(); i++){                                   /* for each element in list */
                                if(Llist.get(i)>max){                                                        /* check if max < any of the element in list */
                                        max = Llist.get(i);                                             /* if yes, update max as the current element */
                                }
                        }
                        return max;                                                                             /* return max */
                }
                else
                        return -1;                                                                                      /* return -1 if the size = 0 */
        }
        
        public static int findTotal(LinkedList<Integer> Llist){           /* define static method findTotal()that accepts linkedlist object */
                int total = 0;                                                                                  /* initialize total as 0 */
                for(int i=0; i<Llist.size(); i++){                                           /* for each element in list */
                        total = total + Llist.get(i);                                           /* add each element to total */
                }
                return total;                                                                                   /* return total */
        }
        
        public static double findAvg(LinkedList<Integer> Llist){  /* define static method findAvg()that accepts linkedlist object */
                double avg = 0.0;                                                                               /* initialize avg as 0.0 */
                if(Llist.size()>0)                                                                           /* check if the size of list is greater than 0 */
                        avg = findTotal(Llist)/(double)Llist.size();            /* calculate average as, average = total/size */
                return avg;                                                                                             /* return average, avg */
        }
        

   public static void main(String args[]) {                                     /* define main() method */
                LinkedList<Integer> linkedlist=new LinkedList<>();          /* create and initialize a linkedlist */
                linkedlist.add(5);                                                                              /* add elements to linkedlist */
                linkedlist.add(8);
                linkedlist.add(13);
                linkedlist.add(7);
                linkedlist.add(15);
                linkedlist.add(2);
                linkedlist.add(9);
                linkedlist.add(22);
                
                System.out.print("Elements : ");                                                /* print all elements in linkedlist */
                for(int i=0; i<linkedlist.size(); i++){
                        System.out.print(linkedlist.get(i)+" ");
                }
                System.out.println();

                System.out.println("Smallest : " + findMin(linkedlist));/* print smallest element in linkedlist using findMin() */
                System.out.println("Largest : " + findMax(linkedlist)); /* print largest element in linkedlist using findMax() */
                System.out.println("Total : " + findTotal(linkedlist)); /* print total using findTotal() */
                System.out.println("Average : " + findAvg(linkedlist)); /* print average using findAvg() */
   }
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an...
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an input of n numbers stored in an array A[1:n]. Compare the following strategies in terms of their exact number of comparisons. Strategy 1: adapt the two separate For loops idea for minimum and maximum. Strategy 2: Run mergesort to sort the numbers in ascending or descending order, then output the 2nd ranked and (n-1)th ranked elements. First write each algorithm in pseudocde, then analyze...
Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
method to remove all elements frrom my linked list (java)
method to remove all elements frrom my linked list (java)
Find the last node of a linked list of n elements whose index is a multiple...
Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then you should return the second 12 (with index 4). Your algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k) LinkedList.java. public...
3. Find the last node of a linked list of n elements whose index is a...
3. Find the last node of a linked list of n elements whose index is a multiple of k (counted from 0). For example, if the list is 12 → 75 → 37 → 99 → 12 → 38 → 99 → 60 ↓ and k = 4, then it should return the second 12 (with index 4). The algorithm should take O(n) time and use O(1) extra space. Implement the following method in LinkedList.java. public T lastK(int k)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT