// Informatik II, Copyright by Institut für Informatik, Universität München
// MUSTERLÖSUNG zu P-22 ohne ausreichend Javadoc, Stand 5.7.04

import java.util.ListIterator;

public class Umdrehen{

    public static void reverse(InfoIIListe l){
	reverseprof(l);
	// reversesimp(l);
	// reverseboese(l);
    }


    /**
     * eine professionelle Lösung ohne java.util.LinkedList
     */
    public static void reverseprof(InfoIIListe l) {
	reverse_aux(l,l.listIterator());
    }

    /** 
	i must sit on l.  reverses the suffix of l starting from i and
	places it at the start of the list overwriting what may be
	there already. The result is an iterator (possibly but not
	necessarily the old one) sitting at the end of this region. The
	contents of the remainder of the list are unspecified. E.g.
	DHR|TU -> UT|XXX
        DH|RTU -> UTR|XX
    */
    private static ListIterator reverse_aux(InfoIIListe l, ListIterator i) {
	if (!i.hasNext())
	    return l.listIterator();
	else {
	    Object x = i.next();
	    ListIterator j = reverse_aux(l,i);
	    j.next();
	    j.set(x);
	    return j;
	}
    }

    /**
     * eine simple Lösung, die sich auf java.util.LinkedList abstützt
     */
    public static void reversesimp(InfoIIListe l){
	ListIterator i;
	java.util.LinkedList hilf = new java.util.LinkedList();
	i = l.listIterator(); 
	while(i.hasNext()) {
	    hilf.addLast(i.next());
	    i.remove();
	}
	i = hilf.listIterator();
	while(i.hasNext()) {
	    l.addFirst(i.next());
	}
    }

    /**
     * eine böse Implementierung, die nicht mit java.util.LinkedList 
     * verträglich ist, wohl aber mit P-23
     */
    public static void reverseboese(InfoIIListe l){
	java.util.ListIterator i = l.listIterator();
	while(i.hasNext()){
	    l.addFirst(i.next());

	    i.remove();
	    // UmdrehenTest wirft java.util.ConcurrentModificationException
	    // geht doch, wenn java.util. aus UmdrehenTest.java gelöscht wird
	}
    }
}
