package elte.java2_utikalauz5.generic; /** Generikus lista. @link.forrásfájl {@docRoot}/../data/generic/src LinkedList.java @since Java 2 Útikalauz programozóknak 5.0 */ class LinkedList implements Collection { protected class Node { A elt; Node next = null; Node(A elt) { this.elt = elt; } } protected Node head = null; protected Node tail = null; public void add(A elt) { if ( head == null ) { head = new Node(elt); tail = head; } else { tail.next = new Node(elt); tail = tail.next; } } public Iterator iterator() { return new Iterator() { protected Node ptr = head; public boolean hasNext() { return ptr != null; } public A next() { if ( ptr == null ) throw new NoSuchElementException(); A elt = ptr.elt; ptr = ptr.next; return elt; } }; } public static void main(String[] args) { LinkedList xs = new LinkedList(); // byte lista xs.add(new Byte("0")); xs.add(new Byte("1")); Byte x = xs.iterator().next(); LinkedList ys = new LinkedList(); // String lista ys.add("zero"); ys.add("one"); String y = ys.iterator().next(); LinkedList> zs = new LinkedList>(); zs.add(ys); // String listák listája String z = zs.iterator().next().iterator().next(); } } interface Collection { public void add(A x); public Iterator iterator(); } interface Iterator { public A next(); public boolean hasNext(); } class NoSuchElementException extends RuntimeException { /** Verziószám */ private final static long serialVersionUID = 15L; }