import java.util.*; class Zelle { E inhalt; Zelle next; Zelle(E el){ inhalt = el;} Zelle(E el, Zelle z){ inhalt = el; next = z;} } class Liste implements Iterable { private Zelle anfang; private Zelle 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, anfang); if (ende == null) ende = anfang; } void einsetzenEnde(E e){ if (ende == null) einsetzenAnfang(e); else { Zelle z = new Zelle(e); ende.next = z; ende = z; } } public String toString(){ String s=""; for(E e : this) s += e + " "; return s; } public Iterator iterator(){ return new Cursor(); } class Cursor implements Iterator{ private Zelle cursorPos=anfang; public boolean hasNext() { return cursorPos!= null; } public E next(){ E elt = cursorPos.inhalt; cursorPos = cursorPos.next; return elt; } public void remove() { throw new UnsupportedOperationException(); } }// Ende der Klasse Iterator } 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 meineCharListe = new Liste('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 meinePrimListe = new Liste(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 myChList = new Liste('H','A','L','L','O'); Liste myPrList = new Liste(2,3,5,7,11,13,17,19); Liste lotto = new Liste(6,23,25,37,38,48); Liste tip = new Liste(2,11,23,26,29,38,48); int treffer = 0; for(int t: tip) for(int l: lotto) if(l==t) treffer++; System.out.println( "Getippt: "+tip+"\n"+"Gezogen: "+lotto+"\n"+ "Treffer: "+treffer); } }