COP 3337 Section U08 Fall 2015 The Lists Class ============== I. The Lists.java class import java.util.Collections; import java.util.LinkedList; import java.util.ListIterator; public class Lists { // sort list in increasing order public static > void sort(LinkedList list) { Collections.sort(list); } // reverse // sort list in increasing order public static > void reverse(LinkedList list) { Collections.reverse(list); } // list is not null // list is sorted in increasing order // if x is not in the list, insert it in its proper place // and return true // if x is in the list, return false public static > boolean insert(T x, LinkedList list) { // form an iterator ListIterator ite = list.listIterator(); // advance to the end of the list or to the first item >= x while (ite.hasNext()) { T current = ite.next(); if (current.equals(x)) return false; if (current.compareTo(x) > 0) { // go back ite.previous(); break; } } // insert here ite.add(x); return true; } public static void print( LinkedList list) { if (list == null || list.size() == 0) System.out.println("The list is empty."); else { for (T curr : list) System.out.println(curr); } } } II. The Driver import java.util.LinkedList; public class ListMethods { // test lists public static void main(String[] args) { System.out.println("Testing Lists"); System.out.println("=============\n\n"); System.out.println("We add the names Papa Bill, Mark," + "Grandpa, Ana, Mark."); LinkedList names = new LinkedList(); Lists.insert("Papa Bill", names); Lists.insert("Mark", names); Lists.insert("Grandpa", names); Lists.insert("Ana", names); Lists.insert("Mark", names); System.out.println("\nWe print the list of names."); Lists.print(names); } } III. The Output Testing Lists ============= We add the names Papa Bill, Mark,Grandpa, Ana, Mark. We print the list of names. Ana Grandpa Mark Papa Bill BUILD SUCCESSFUL (total time: 0 seconds)