1 public class LinkedListElement {
2 public LinkedListElement next;
3 public LinkedListElement prev;
6 public LinkedListElement(Object e,
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 boolean isEmpty() {
66 public Object clone() {
67 System.out.println("LinkedList.clone() not implemented.");
71 public boolean contains(Object o) {
72 LinkedListElement e = head;
75 if (e.element==null) {
83 if (o.equals(e.element)) {
92 public Object getFirst() {
99 public Object getLast() {
106 public Object element() {
110 public Object peek() {
114 public Object peekFirst() {
118 public Object peekLast() {
122 public Object removeFirst() {
124 System.out.println("LinkedList: illegal removeFirst()");
127 Object o = head.element;
138 public Object removeLast() {
140 System.out.println("LinkedList: illegal removeLast()");
143 Object o = tail.element;
154 public void remove(Object o) {
156 System.out.println("LinkedList: illegal remove( Object o )");
159 LinkedListElement e = head;
161 if( e.element == o ) {
162 if( e.prev != null ) {
163 e.prev.next = e.next;
165 if( e.next != null ) {
166 e.next.prev = e.prev;
173 System.out.println("LinkedList: illegal remove( Object o ), "+o+" not found");
177 public Object pop() {
178 Object o = getFirst();
183 public void push(Object o) {
187 public Iterator iterator() {
188 return new LinkedListIterator(this);
192 public class LinkedListIterator extends Iterator {
194 LinkedListElement itr;
197 public LinkedListIterator(LinkedList ll) {
203 public boolean hasNext() {
207 public Object next() {
209 System.out.println("LinkedListIterator: illegal next()");
212 removeable = itr.element;
217 public void remove() {
218 if( removeable == null ) {
219 System.out.println("LinkedListIterator: illegal remove()");
222 ll.remove(removeable);