//Working with a linked list and listIterator import java.util.*; class ListDriver { public static void main (String[] args) { LinkedList myList1 = new LinkedList(); myList1.add (new Integer(8)); myList1.add (new Integer(56)); myList1.addLast (new Integer(78)); ArrayList myList2 = new ArrayList(myList1); myList2.add(new Integer(89)); myList2.add(new Integer(76)); // using the iterator to scan thru' the list // from the head to the tail and print out contents System.out.println("List traversed forward"); for (ListIterator i = myList2.listIterator(); i.hasNext();) System.out.println((Integer)(i.next())); boolean x = replace(myList2, new Integer(76), new Integer(77)); System.out.println(" List in the reverse"); ListIterator j = myList2.listIterator(myList2.size()); while (j.hasPrevious()) System.out.println((Integer)j.previous()); } public static boolean replace(List lst, Object obj1, Object obj2) { if (obj1 == null) return false; ListIterator i = lst.listIterator(); while (i.hasNext()) { if (((Integer)obj1).equals ((Integer)i.next())) { //replace with obj2 i.previous(); i.set(obj2); return true; } } // ASSERT : list has run out return false; } } /** * Homework 3: Due: 11/2/2000 in class for A section 11/3 for B section 1. Write a static method "listSort( List lst)" that sorts the list passed in as a parameter. Add statements to the main method given above that tests the listSort method. (Type it in, compile and execute.. make sure your code works.) 2. Write a static method that determines the sum of odd numbers in a list passed in as a parameter. "integer oddSum(List lst)" Add statements to the main method given above that tests the oddSum method. (Type it in, compile and execute.. make sure your code works.) 3. Write another static method that "void twister(List lst)" that exchanges first and last element, first+1 and last-1 element, first+2 and last-2 element etc. If there are odd number of elements the middle element is not exchanged with anything. Type in your method, test it by adding statements to the driver program. */