import java.util.*;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
public class LinkedListProgram
{
public static void main(String[] args) throws IOException
{
int i,number, ran;
Scanner sc = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<Integer>();
ListIterator li;
String line;
Random random = new Random();
int pick = random.nextInt(150);
System.out.println("Enter # of nodes");
number = sc.nextInt();
ran = (1+ (int)(Math.random()*number));
if (number > 0)
{
for (i = 0; i < number; i++)
list.add(1+ (int)(Math.random()*150));
Collections.sort(list);
li = list.listIterator();
String s = list.toString();
System.out.println();
System.out.println(" Index Node");
while (li.hasNext())
{
System.out.println(" " + li.nextIndex() + ". " + li.next());
}
System.out.println("\n" + " " + ran);
}
else
System.out.println("\nnumber is less than 0\n");
}
}
Right now my program lets a user type in a number and that many random numbers will be added to a linked list.
What I need help with is how to calculate the sum, mean, range and median of the numbers in the list. Would converting the list to an array be the most logical way? Is there a way I can do this via list iterator commands? I'm not very good with this.