Quiz 12 (Exercise for Chap. 19: Linked List and iterator)

/**
   A program to try out LinkedList
 */
import java.util.LinkedList ;
import java.util.ListIterator ;

public class ListTest2
{
    public static void main(String[] args)
    {
	LinkedList list = new LinkedList() ;
	list.add("A") ;
	list.add("B") ;
	list.add("C") ;
	list.add("D") ;
	list.add("E") ;
	System.out.println(list) ;

	ListIterator iterator = list.listIterator() ;
	while( iterator.hasNext()) {
	    System.out.print(iterator.next()) ;
	}
	System.out.println() ;

	while( iterator.hasPrevious()) {
	    System.out.print(iterator.previous()) ;
	}
	System.out.println() ;

	list.add(3, "3") ;
	list.add(5, "5") ;
	list.set(2, "2") ;
	System.out.println(list) ;

	String x = (String) list.get(3) ;
	list.add(x) ;
	System.out.println(list) ;

	String y = (String) list.remove(1) ;
	list.set(4, y) ;
	System.out.println(list) ;

	if (list.contains(x)) list.remove(list.indexOf(x)) ;
	System.out.println(list) ;

	iterator = list.listIterator() ;
	iterator.next() ;
	iterator.next() ;
	iterator.next() ;
	iterator.remove() ;
	System.out.println(list) ;

	iterator.next() ;
	iterator.set("X") ;
	iterator.previous() ;
	iterator.remove() ;
	System.out.println(list) ;

	iterator.next() ;
	iterator.next() ;
	iterator.add("Z") ;
	iterator.add("Y") ;
	System.out.println(list) ;

    }
}