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 Iterator() { private Zelle itCursor; private Zelle 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 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); } }