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 void add(Object o) {
26 head = new LinkedListElement(o, null, null);
30 tail.next = new LinkedListElement(o, null, tail);
36 public void addFirst(Object o) {
38 head = new LinkedListElement(o, null, null);
42 head.prev = new LinkedListElement(o, head, null);
48 public void 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 boolean remove(Object o) {
156 // System.out.println("LinkedList: illegal remove( Object o )");
160 LinkedListElement e = head;
162 if (e.element == o) {
163 if (e.prev != null) {
164 e.prev.next = e.next;
166 if (e.next != null) {
167 e.next.prev = e.prev;
174 // System.out.println("LinkedList: illegal remove( Object o ), "+o+" not found");
179 public Object pop() {
180 Object o = getFirst();
185 public void push(Object o) {
189 public Iterator iterator() {
190 return new LinkedListIterator(this);
194 public class LinkedListIterator extends Iterator {
196 LinkedListElement itr;
199 public LinkedListIterator(LinkedList ll) {
205 public boolean hasNext() {
209 public Object next() {
211 System.out.println("LinkedListIterator: illegal next()");
214 removeable = itr.element;
219 public void remove() {
220 if( removeable == null ) {
221 System.out.println("LinkedListIterator: illegal remove()");
224 ll.remove(removeable);