In: Computer Science
Find the largest and smallest element of a linked list, print total of all elements and find out the average.
Code needed in java
We can use java.util.LinkedList class for creating a linkedList and implement the program as follows:
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.