import java.util.*;

class Zelle <E> {
	E inhalt;
	Zelle<E> next;
	Zelle(E el){ inhalt = el;}
	Zelle(E el, Zelle<E> z){ inhalt = el; next = z;}
}

class Liste <E> implements Iterable<E> {
	private Zelle<E> anfang;
	private Zelle<E> ende;
	
	Liste(E ... es){ for(E e:es) einsetzenEnde(e); }
	
	boolean istLeer(){ return anfang == null;}
	
	int laenge(){ 
		int l = 0;
		for (E e : this) l++; 
		return l;
		}
		 
	int suche(E e){ 
		int l = 0;
		for (E el : this) {if (el == e) return l; l++;}
		return -1;
		} 
		
	
		
	void einsetzenAnfang(E e){ 
		anfang = new Zelle<E>(e, anfang);
		if (ende == null) ende = anfang;
		}
		
	void einsetzenEnde(E e){ 
		if (ende == null) einsetzenAnfang(e);
		else {
		   Zelle<E> z = new Zelle<E>(e);
		   ende.next = z;
		   ende = z;
		   }
		}
		
	public String toString(){
      String s="";
      for(E e : this) s += e + " ";
      return s;
    }	
			
  public Iterator<E> iterator(){
    	
    	return new Iterator<E>() { 
    	    private Zelle<E> itCursor;
    	    private Zelle<E> itLast;
    	    private boolean first = true;
    	    private boolean removed = false;
    	    		   
            public boolean hasNext() {
            	if (first) return !istLeer(); 
            	if (itCursor == null) return false;
            	if (itCursor.next == null) return false;
            	return true; 
            	}
	    
	    public E next() { 
	    	if (first){
	    	    first = false;
	    	    itLast = itCursor = anfang;
	    	    if (itCursor == null) return null;
	    	    return itCursor.inhalt;
	    	    }	 
            	if ((itCursor == null)||(itCursor.next == null))   throw new NoSuchElementException(); 
            	itLast = itCursor;
            	itCursor = itCursor.next;
            	removed = false;
            	return itCursor.inhalt; 
            	}
            
            public void remove() {
            	if (first || removed || (itCursor == null) || (itLast == null)) throw new  IllegalStateException();  
            	itLast.next  = itCursor.next;
            	removed = true;
            	}
	    };
	 }   

        
}
     
public class ListeT {

    public static void main(String args[]) {
        System.out.println("Listen Tester - Test 1");
        System.out.println("");
        
        Character cs  = 'S'; 
        Integer ps1  = 13;
        Integer ps2  = 42;
        
        
        Liste<Character> meineCharListe = new Liste<Character>('B','E','I', cs, 'P','I','E','L','!');
        meineCharListe.einsetzenAnfang('B'); meineCharListe.einsetzenEnde('E');
        meineCharListe.einsetzenEnde('I'); meineCharListe.einsetzenEnde(cs);
        meineCharListe.einsetzenEnde('P'); meineCharListe.einsetzenEnde('I');
        meineCharListe.einsetzenEnde('E'); meineCharListe.einsetzenEnde('L');
        meineCharListe.einsetzenEnde('!');
        
        Liste<Integer> meinePrimListe = new Liste<Integer>(2,3,5,7,11,13,17,19,23);
                    
        System.out.println(meineCharListe);
        System.out.println("Länge der Liste: " + meineCharListe.laenge());
        System.out.println("Suche: " + cs);
        int cindex = meineCharListe.suche(cs);
        if (cindex < 0) System.out.println("Nicht gefunden!" );
        else System.out.println("Gefunden auf Position: " + cindex);
        System.out.println();
        
        System.out.println(meinePrimListe);
        System.out.println("Länge der Liste: " + meinePrimListe.laenge());
        System.out.println("Suche: " + ps1);
        int pindex = meinePrimListe.suche(ps1);
        if (pindex < 0) System.out.println("Nicht gefunden!" );
        else System.out.println("Gefunden auf Position: " + pindex);
        System.out.println("Suche: " + ps2);
        pindex = meinePrimListe.suche(ps2);
        if (pindex < 0) System.out.println("Nicht gefunden!" );
        else System.out.println("Gefunden auf Position: " + pindex);
        
        Liste<Character> myChList = new Liste<Character>('H','A','L','L','O');
	      Liste<Integer> myPrList = new Liste<Integer>(2,3,5,7,11,13,17,19);
     }
}
