1 public class LinkedListElement {
2 public LinkedListElement next;
3 public LinkedListElement prev;
6 public LinkedListElement( Object e,
8 LinkedListElement p ) {
15 public class LinkedList {
16 LinkedListElement head;
17 LinkedListElement tail;
24 public add( Object o ) {
26 head = new LinkedListElement( o, null, null );
30 tail.next = new LinkedListElement( o, null, tail );
36 public addFirst( Object o ) {
38 head = new LinkedListElement( o, null, null );
42 head.prev = new LinkedListElement( o, head, null );
48 public addLast( Object o ) {
62 public Object clone() {
63 System.out.println( "LinkedList.clone() not implemented." );
67 public boolean contains( Object o ) {
68 LinkedListElement e = head;
70 if( e.element == o ) {
78 public Object getFirst() {
85 public Object getLast() {
92 public Object element() {
96 public Object peek() {
100 public Object peekFirst() {
104 public Object peekLast() {
108 public void removeFirst() {
110 System.out.println( "LinkedList: illegal removeFirst()" );
120 public void removeLast() {
122 System.out.println( "LinkedList: illegal removeLast()" );
132 public void remove( Object o ) {
134 System.out.println( "LinkedList: illegal remove( Object o )" );
137 LinkedListElement e = head;
139 if( e.element == o ) {
140 if( e.prev != null ) {
141 e.prev.next = e.next;
143 if( e.next != null ) {
144 e.next.prev = e.prev;
151 System.out.println( "LinkedList: illegal remove( Object o ), "+o+" not found" );
155 public Object pop() {
156 Object o = getFirst();
161 public void push( Object o ) {
165 public Iterator iterator() {
166 return new LinkedListIterator( this );
170 public class LinkedListIterator extends Iterator {
171 LinkedListElement itr;
173 public LinkedListIterator( LinkedList ll ) {
183 System.out.println( "LinkedListIterator: illegal next()" );
186 Object o = itr.element;