How would I do an additional method to print the output in an ordered, ascending sequence?
import java.util.*;
import java.util.Scanner;
import java.io.*;
public class LinLinkedList{
public static void main(String [] args){
LinkedList list = new LinkedList();
Scanner in = new Scanner(System.in);
int n = 0;
while (!(n<0)){
System.out.println("Enter a number: ");
n = in.nextInt();
list.add(n);
}
System.out.println(list);
}
}
Adding
if(n>0)just beforelist.add(n);will solve it. Then you will have only positive values in your linked list. To sort your list in ascending order, you can useCollections.sort(list);as Guiilaume Polet suggested. After the while loop, use this sorting method and then print the contents of the linked list.