libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / util / LinkedList.java
blob3f8b2ad60a073b265b97ec9d7abd8d32d7e72c0e
1 /* LinkedList.java -- Linked list implementation of the List interface
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
40 import java.io.IOException;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutputStream;
43 import java.io.Serializable;
44 import java.lang.reflect.Array;
46 /**
47 * Linked list implementation of the List interface. In addition to the
48 * methods of the List interface, this class provides access to the first
49 * and last list elements in O(1) time for easy stack, queue, or double-ended
50 * queue (deque) creation. The list is doubly-linked, with traversal to a
51 * given index starting from the end closest to the element.<p>
53 * LinkedList is not synchronized, so if you need multi-threaded access,
54 * consider using:<br>
55 * <code>List l = Collections.synchronizedList(new LinkedList(...));</code>
56 * <p>
58 * The iterators are <i>fail-fast</i>, meaning that any structural
59 * modification, except for <code>remove()</code> called on the iterator
60 * itself, cause the iterator to throw a
61 * {@link ConcurrentModificationException} rather than exhibit
62 * non-deterministic behavior.
64 * @author Original author unknown
65 * @author Bryce McKinlay
66 * @author Eric Blake (ebb9@email.byu.edu)
67 * @author Tom Tromey (tromey@redhat.com)
68 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
69 * @see List
70 * @see ArrayList
71 * @see Vector
72 * @see Collections#synchronizedList(List)
73 * @since 1.2
74 * @status Complete to 1.6
76 public class LinkedList<T> extends AbstractSequentialList<T>
77 implements List<T>, Deque<T>, Cloneable, Serializable
79 /**
80 * Compatible with JDK 1.2.
82 private static final long serialVersionUID = 876323262645176354L;
84 /**
85 * The first element in the list.
87 transient Entry<T> first;
89 /**
90 * The last element in the list.
92 transient Entry<T> last;
94 /**
95 * The current length of the list.
97 transient int size = 0;
99 /**
100 * Class to represent an entry in the list. Holds a single element.
102 private static final class Entry<T>
104 /** The element in the list. */
105 T data;
107 /** The next list entry, null if this is last. */
108 Entry<T> next;
110 /** The previous list entry, null if this is first. */
111 Entry<T> previous;
114 * Construct an entry.
115 * @param data the list element
117 Entry(T data)
119 this.data = data;
121 } // class Entry
124 * Obtain the Entry at a given position in a list. This method of course
125 * takes linear time, but it is intelligent enough to take the shorter of the
126 * paths to get to the Entry required. This implies that the first or last
127 * entry in the list is obtained in constant time, which is a very desirable
128 * property.
129 * For speed and flexibility, range checking is not done in this method:
130 * Incorrect values will be returned if (n &lt; 0) or (n &gt;= size).
132 * @param n the number of the entry to get
133 * @return the entry at position n
135 // Package visible for use in nested classes.
136 Entry<T> getEntry(int n)
138 Entry<T> e;
139 if (n < size / 2)
141 e = first;
142 // n less than size/2, iterate from start
143 while (n-- > 0)
144 e = e.next;
146 else
148 e = last;
149 // n greater than size/2, iterate from end
150 while (++n < size)
151 e = e.previous;
153 return e;
157 * Remove an entry from the list. This will adjust size and deal with
158 * `first' and `last' appropriatly.
160 * @param e the entry to remove
162 // Package visible for use in nested classes.
163 void removeEntry(Entry<T> e)
165 modCount++;
166 size--;
167 if (size == 0)
168 first = last = null;
169 else
171 if (e == first)
173 first = e.next;
174 e.next.previous = null;
176 else if (e == last)
178 last = e.previous;
179 e.previous.next = null;
181 else
183 e.next.previous = e.previous;
184 e.previous.next = e.next;
190 * Checks that the index is in the range of possible elements (inclusive).
192 * @param index the index to check
193 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size
195 private void checkBoundsInclusive(int index)
197 if (index < 0 || index > size)
198 throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
199 + size);
203 * Checks that the index is in the range of existing elements (exclusive).
205 * @param index the index to check
206 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size
208 private void checkBoundsExclusive(int index)
210 if (index < 0 || index >= size)
211 throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
212 + size);
216 * Create an empty linked list.
218 public LinkedList()
223 * Create a linked list containing the elements, in order, of a given
224 * collection.
226 * @param c the collection to populate this list from
227 * @throws NullPointerException if c is null
229 public LinkedList(Collection<? extends T> c)
231 addAll(c);
235 * Returns the first element in the list.
237 * @return the first list element
238 * @throws NoSuchElementException if the list is empty
240 public T getFirst()
242 if (size == 0)
243 throw new NoSuchElementException();
244 return first.data;
248 * Returns the last element in the list.
250 * @return the last list element
251 * @throws NoSuchElementException if the list is empty
253 public T getLast()
255 if (size == 0)
256 throw new NoSuchElementException();
257 return last.data;
261 * Remove and return the first element in the list.
263 * @return the former first element in the list
264 * @throws NoSuchElementException if the list is empty
266 public T removeFirst()
268 if (size == 0)
269 throw new NoSuchElementException();
270 modCount++;
271 size--;
272 T r = first.data;
274 if (first.next != null)
275 first.next.previous = null;
276 else
277 last = null;
279 first = first.next;
281 return r;
285 * Remove and return the last element in the list.
287 * @return the former last element in the list
288 * @throws NoSuchElementException if the list is empty
290 public T removeLast()
292 if (size == 0)
293 throw new NoSuchElementException();
294 modCount++;
295 size--;
296 T r = last.data;
298 if (last.previous != null)
299 last.previous.next = null;
300 else
301 first = null;
303 last = last.previous;
305 return r;
309 * Insert an element at the first of the list.
311 * @param o the element to insert
313 public void addFirst(T o)
315 Entry<T> e = new Entry(o);
317 modCount++;
318 if (size == 0)
319 first = last = e;
320 else
322 e.next = first;
323 first.previous = e;
324 first = e;
326 size++;
330 * Insert an element at the last of the list.
332 * @param o the element to insert
334 public void addLast(T o)
336 addLastEntry(new Entry<T>(o));
340 * Inserts an element at the end of the list.
342 * @param e the entry to add
344 private void addLastEntry(Entry<T> e)
346 modCount++;
347 if (size == 0)
348 first = last = e;
349 else
351 e.previous = last;
352 last.next = e;
353 last = e;
355 size++;
359 * Returns true if the list contains the given object. Comparison is done by
360 * <code>o == null ? e = null : o.equals(e)</code>.
362 * @param o the element to look for
363 * @return true if it is found
365 public boolean contains(Object o)
367 Entry<T> e = first;
368 while (e != null)
370 if (equals(o, e.data))
371 return true;
372 e = e.next;
374 return false;
378 * Returns the size of the list.
380 * @return the list size
382 public int size()
384 return size;
388 * Adds an element to the end of the list.
390 * @param o the entry to add
391 * @return true, as it always succeeds
393 public boolean add(T o)
395 addLastEntry(new Entry<T>(o));
396 return true;
400 * Removes the entry at the lowest index in the list that matches the given
401 * object, comparing by <code>o == null ? e = null : o.equals(e)</code>.
403 * @param o the object to remove
404 * @return true if an instance of the object was removed
406 public boolean remove(Object o)
408 Entry<T> e = first;
409 while (e != null)
411 if (equals(o, e.data))
413 removeEntry(e);
414 return true;
416 e = e.next;
418 return false;
422 * Append the elements of the collection in iteration order to the end of
423 * this list. If this list is modified externally (for example, if this
424 * list is the collection), behavior is unspecified.
426 * @param c the collection to append
427 * @return true if the list was modified
428 * @throws NullPointerException if c is null
430 public boolean addAll(Collection<? extends T> c)
432 return addAll(size, c);
436 * Insert the elements of the collection in iteration order at the given
437 * index of this list. If this list is modified externally (for example,
438 * if this list is the collection), behavior is unspecified.
440 * @param c the collection to append
441 * @return true if the list was modified
442 * @throws NullPointerException if c is null
443 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
445 public boolean addAll(int index, Collection<? extends T> c)
447 checkBoundsInclusive(index);
448 int csize = c.size();
450 if (csize == 0)
451 return false;
453 Iterator<? extends T> itr = c.iterator();
455 // Get the entries just before and after index. If index is at the start
456 // of the list, BEFORE is null. If index is at the end of the list, AFTER
457 // is null. If the list is empty, both are null.
458 Entry<T> after = null;
459 Entry<T> before = null;
460 if (index != size)
462 after = getEntry(index);
463 before = after.previous;
465 else
466 before = last;
468 // Create the first new entry. We do not yet set the link from `before'
469 // to the first entry, in order to deal with the case where (c == this).
470 // [Actually, we don't have to handle this case to fufill the
471 // contract for addAll(), but Sun's implementation appears to.]
472 Entry<T> e = new Entry<T>(itr.next());
473 e.previous = before;
474 Entry<T> prev = e;
475 Entry<T> firstNew = e;
477 // Create and link all the remaining entries.
478 for (int pos = 1; pos < csize; pos++)
480 e = new Entry<T>(itr.next());
481 e.previous = prev;
482 prev.next = e;
483 prev = e;
486 // Link the new chain of entries into the list.
487 modCount++;
488 size += csize;
489 prev.next = after;
490 if (after != null)
491 after.previous = e;
492 else
493 last = e;
495 if (before != null)
496 before.next = firstNew;
497 else
498 first = firstNew;
499 return true;
503 * Remove all elements from this list.
505 public void clear()
507 if (size > 0)
509 modCount++;
510 first = null;
511 last = null;
512 size = 0;
517 * Return the element at index.
519 * @param index the place to look
520 * @return the element at index
521 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
523 public T get(int index)
525 checkBoundsExclusive(index);
526 return getEntry(index).data;
530 * Replace the element at the given location in the list.
532 * @param index which index to change
533 * @param o the new element
534 * @return the prior element
535 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
537 public T set(int index, T o)
539 checkBoundsExclusive(index);
540 Entry<T> e = getEntry(index);
541 T old = e.data;
542 e.data = o;
543 return old;
547 * Inserts an element in the given position in the list.
549 * @param index where to insert the element
550 * @param o the element to insert
551 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
553 public void add(int index, T o)
555 checkBoundsInclusive(index);
556 Entry<T> e = new Entry<T>(o);
558 if (index < size)
560 modCount++;
561 Entry<T> after = getEntry(index);
562 e.next = after;
563 e.previous = after.previous;
564 if (after.previous == null)
565 first = e;
566 else
567 after.previous.next = e;
568 after.previous = e;
569 size++;
571 else
572 addLastEntry(e);
576 * Removes the element at the given position from the list.
578 * @param index the location of the element to remove
579 * @return the removed element
580 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
582 public T remove(int index)
584 checkBoundsExclusive(index);
585 Entry<T> e = getEntry(index);
586 removeEntry(e);
587 return e.data;
591 * Returns the first index where the element is located in the list, or -1.
593 * @param o the element to look for
594 * @return its position, or -1 if not found
596 public int indexOf(Object o)
598 int index = 0;
599 Entry<T> e = first;
600 while (e != null)
602 if (equals(o, e.data))
603 return index;
604 index++;
605 e = e.next;
607 return -1;
611 * Returns the last index where the element is located in the list, or -1.
613 * @param o the element to look for
614 * @return its position, or -1 if not found
616 public int lastIndexOf(Object o)
618 int index = size - 1;
619 Entry<T> e = last;
620 while (e != null)
622 if (equals(o, e.data))
623 return index;
624 index--;
625 e = e.previous;
627 return -1;
631 * Obtain a ListIterator over this list, starting at a given index. The
632 * ListIterator returned by this method supports the add, remove and set
633 * methods.
635 * @param index the index of the element to be returned by the first call to
636 * next(), or size() to be initially positioned at the end of the list
637 * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
639 public ListIterator<T> listIterator(int index)
641 checkBoundsInclusive(index);
642 return new LinkedListItr<T>(index);
646 * Create a shallow copy of this LinkedList (the elements are not cloned).
648 * @return an object of the same class as this object, containing the
649 * same elements in the same order
651 public Object clone()
653 LinkedList<T> copy = null;
656 copy = (LinkedList<T>) super.clone();
658 catch (CloneNotSupportedException ex)
661 copy.clear();
662 copy.addAll(this);
663 return copy;
667 * Returns an array which contains the elements of the list in order.
669 * @return an array containing the list elements
671 public Object[] toArray()
673 Object[] array = new Object[size];
674 Entry<T> e = first;
675 for (int i = 0; i < size; i++)
677 array[i] = e.data;
678 e = e.next;
680 return array;
684 * Returns an Array whose component type is the runtime component type of
685 * the passed-in Array. The returned Array is populated with all of the
686 * elements in this LinkedList. If the passed-in Array is not large enough
687 * to store all of the elements in this List, a new Array will be created
688 * and returned; if the passed-in Array is <i>larger</i> than the size
689 * of this List, then size() index will be set to null.
691 * @param a the passed-in Array
692 * @return an array representation of this list
693 * @throws ArrayStoreException if the runtime type of a does not allow
694 * an element in this list
695 * @throws NullPointerException if a is null
697 public <S> S[] toArray(S[] a)
699 if (a.length < size)
700 a = (S[]) Array.newInstance(a.getClass().getComponentType(), size);
701 else if (a.length > size)
702 a[size] = null;
703 Entry<T> e = first;
704 for (int i = 0; i < size; i++)
706 a[i] = (S) e.data;
707 e = e.next;
709 return a;
713 * Adds the specified element to the end of the list.
715 * @param value the value to add.
716 * @return true.
717 * @since 1.5
719 public boolean offer(T value)
721 return add(value);
725 * Returns the first element of the list without removing
726 * it.
728 * @return the first element of the list.
729 * @throws NoSuchElementException if the list is empty.
730 * @since 1.5
732 public T element()
734 return getFirst();
738 * Returns the first element of the list without removing
739 * it.
741 * @return the first element of the list, or <code>null</code>
742 * if the list is empty.
743 * @since 1.5
745 public T peek()
747 if (size == 0)
748 return null;
749 return getFirst();
753 * Removes and returns the first element of the list.
755 * @return the first element of the list, or <code>null</code>
756 * if the list is empty.
757 * @since 1.5
759 public T poll()
761 if (size == 0)
762 return null;
763 return removeFirst();
767 * Removes and returns the first element of the list.
769 * @return the first element of the list.
770 * @throws NoSuchElementException if the list is empty.
771 * @since 1.5
773 public T remove()
775 return removeFirst();
779 * Serializes this object to the given stream.
781 * @param s the stream to write to
782 * @throws IOException if the underlying stream fails
783 * @serialData the size of the list (int), followed by all the elements
784 * (Object) in proper order
786 private void writeObject(ObjectOutputStream s) throws IOException
788 s.defaultWriteObject();
789 s.writeInt(size);
790 Entry<T> e = first;
791 while (e != null)
793 s.writeObject(e.data);
794 e = e.next;
799 * Deserializes this object from the given stream.
801 * @param s the stream to read from
802 * @throws ClassNotFoundException if the underlying stream fails
803 * @throws IOException if the underlying stream fails
804 * @serialData the size of the list (int), followed by all the elements
805 * (Object) in proper order
807 private void readObject(ObjectInputStream s)
808 throws IOException, ClassNotFoundException
810 s.defaultReadObject();
811 int i = s.readInt();
812 while (--i >= 0)
813 addLastEntry(new Entry<T>((T) s.readObject()));
817 * A ListIterator over the list. This class keeps track of its
818 * position in the list and the two list entries it is between.
820 * @author Original author unknown
821 * @author Eric Blake (ebb9@email.byu.edu)
823 private final class LinkedListItr<I>
824 implements ListIterator<I>
826 /** Number of modifications we know about. */
827 private int knownMod = modCount;
829 /** Entry that will be returned by next(). */
830 private Entry<I> next;
832 /** Entry that will be returned by previous(). */
833 private Entry<I> previous;
835 /** Entry that will be affected by remove() or set(). */
836 private Entry<I> lastReturned;
838 /** Index of `next'. */
839 private int position;
842 * Initialize the iterator.
844 * @param index the initial index
846 LinkedListItr(int index)
848 if (index == size)
850 next = null;
851 previous = (Entry<I>) last;
853 else
855 next = (Entry<I>) getEntry(index);
856 previous = next.previous;
858 position = index;
862 * Checks for iterator consistency.
864 * @throws ConcurrentModificationException if the list was modified
866 private void checkMod()
868 if (knownMod != modCount)
869 throw new ConcurrentModificationException();
873 * Returns the index of the next element.
875 * @return the next index
877 public int nextIndex()
879 return position;
883 * Returns the index of the previous element.
885 * @return the previous index
887 public int previousIndex()
889 return position - 1;
893 * Returns true if more elements exist via next.
895 * @return true if next will succeed
897 public boolean hasNext()
899 return (next != null);
903 * Returns true if more elements exist via previous.
905 * @return true if previous will succeed
907 public boolean hasPrevious()
909 return (previous != null);
913 * Returns the next element.
915 * @return the next element
916 * @throws ConcurrentModificationException if the list was modified
917 * @throws NoSuchElementException if there is no next
919 public I next()
921 checkMod();
922 if (next == null)
923 throw new NoSuchElementException();
924 position++;
925 lastReturned = previous = next;
926 next = lastReturned.next;
927 return lastReturned.data;
931 * Returns the previous element.
933 * @return the previous element
934 * @throws ConcurrentModificationException if the list was modified
935 * @throws NoSuchElementException if there is no previous
937 public I previous()
939 checkMod();
940 if (previous == null)
941 throw new NoSuchElementException();
942 position--;
943 lastReturned = next = previous;
944 previous = lastReturned.previous;
945 return lastReturned.data;
949 * Remove the most recently returned element from the list.
951 * @throws ConcurrentModificationException if the list was modified
952 * @throws IllegalStateException if there was no last element
954 public void remove()
956 checkMod();
957 if (lastReturned == null)
958 throw new IllegalStateException();
960 // Adjust the position to before the removed element, if the element
961 // being removed is behind the cursor.
962 if (lastReturned == previous)
963 position--;
965 next = lastReturned.next;
966 previous = lastReturned.previous;
967 removeEntry((Entry<T>) lastReturned);
968 knownMod++;
970 lastReturned = null;
974 * Adds an element between the previous and next, and advance to the next.
976 * @param o the element to add
977 * @throws ConcurrentModificationException if the list was modified
979 public void add(I o)
981 checkMod();
982 modCount++;
983 knownMod++;
984 size++;
985 position++;
986 Entry<I> e = new Entry<I>(o);
987 e.previous = previous;
988 e.next = next;
990 if (previous != null)
991 previous.next = e;
992 else
993 first = (Entry<T>) e;
995 if (next != null)
996 next.previous = e;
997 else
998 last = (Entry<T>) e;
1000 previous = e;
1001 lastReturned = null;
1005 * Changes the contents of the element most recently returned.
1007 * @param o the new element
1008 * @throws ConcurrentModificationException if the list was modified
1009 * @throws IllegalStateException if there was no last element
1011 public void set(I o)
1013 checkMod();
1014 if (lastReturned == null)
1015 throw new IllegalStateException();
1016 lastReturned.data = o;
1018 } // class LinkedListItr
1021 * Obtain an Iterator over this list in reverse sequential order.
1023 * @return an Iterator over the elements of the list in
1024 * reverse order.
1025 * @since 1.6
1027 public Iterator<T> descendingIterator()
1029 return new Iterator<T>()
1031 /** Number of modifications we know about. */
1032 private int knownMod = modCount;
1034 /** Entry that will be returned by next(). */
1035 private Entry<T> next = last;
1037 /** Entry that will be affected by remove() or set(). */
1038 private Entry<T> lastReturned;
1040 /** Index of `next'. */
1041 private int position = size() - 1;
1043 // This will get inlined, since it is private.
1045 * Checks for modifications made to the list from
1046 * elsewhere while iteration is in progress.
1048 * @throws ConcurrentModificationException if the
1049 * list has been modified elsewhere.
1051 private void checkMod()
1053 if (knownMod != modCount)
1054 throw new ConcurrentModificationException();
1058 * Tests to see if there are any more objects to
1059 * return.
1061 * @return true if the start of the list has not yet been
1062 * reached.
1064 public boolean hasNext()
1066 return next != null;
1070 * Retrieves the next object from the list.
1072 * @return The next object.
1073 * @throws NoSuchElementException if there are
1074 * no more objects to retrieve.
1075 * @throws ConcurrentModificationException if the
1076 * list has been modified elsewhere.
1078 public T next()
1080 checkMod();
1081 if (next == null)
1082 throw new NoSuchElementException();
1083 --position;
1084 lastReturned = next;
1085 next = lastReturned.previous;
1086 return lastReturned.data;
1090 * Removes the last object retrieved by <code>next()</code>
1091 * from the list, if the list supports object removal.
1093 * @throws ConcurrentModificationException if the list
1094 * has been modified elsewhere.
1095 * @throws IllegalStateException if the iterator is positioned
1096 * before the start of the list or the last object has already
1097 * been removed.
1099 public void remove()
1101 checkMod();
1102 if (lastReturned == null)
1103 throw new IllegalStateException();
1104 removeEntry(lastReturned);
1105 lastReturned = null;
1106 ++knownMod;
1112 * Inserts the specified element at the front of the list.
1114 * @param value the element to insert.
1115 * @return true.
1116 * @since 1.6
1118 public boolean offerFirst(T value)
1120 addFirst(value);
1121 return true;
1125 * Inserts the specified element at the end of the list.
1127 * @param value the element to insert.
1128 * @return true.
1129 * @since 1.6
1131 public boolean offerLast(T value)
1133 return add(value);
1137 * Returns the first element of the list without removing
1138 * it.
1140 * @return the first element of the list, or <code>null</code>
1141 * if the list is empty.
1142 * @since 1.6
1144 public T peekFirst()
1146 return peek();
1150 * Returns the last element of the list without removing
1151 * it.
1153 * @return the last element of the list, or <code>null</code>
1154 * if the list is empty.
1155 * @since 1.6
1157 public T peekLast()
1159 if (size == 0)
1160 return null;
1161 return getLast();
1165 * Removes and returns the first element of the list.
1167 * @return the first element of the list, or <code>null</code>
1168 * if the list is empty.
1169 * @since 1.6
1171 public T pollFirst()
1173 return poll();
1177 * Removes and returns the last element of the list.
1179 * @return the last element of the list, or <code>null</code>
1180 * if the list is empty.
1181 * @since 1.6
1183 public T pollLast()
1185 if (size == 0)
1186 return null;
1187 return removeLast();
1191 * Pops an element from the stack by removing and returning
1192 * the first element in the list. This is equivalent to
1193 * calling {@link #removeFirst()}.
1195 * @return the top of the stack, which is the first element
1196 * of the list.
1197 * @throws NoSuchElementException if the list is empty.
1198 * @since 1.6
1199 * @see #removeFirst()
1201 public T pop()
1203 return removeFirst();
1207 * Pushes an element on to the stack by adding it to the
1208 * front of the list. This is equivalent to calling
1209 * {@link #addFirst(T)}.
1211 * @param value the element to push on to the stack.
1212 * @throws NoSuchElementException if the list is empty.
1213 * @since 1.6
1214 * @see #addFirst(T)
1216 public void push(T value)
1218 addFirst(value);
1222 * Removes the first occurrence of the specified element
1223 * from the list, when traversing the list from head to
1224 * tail. The list is unchanged if the element is not found.
1225 * This is equivalent to calling {@link #remove(Object)}.
1227 * @param o the element to remove.
1228 * @return true if an instance of the object was removed.
1229 * @since 1.6
1231 public boolean removeFirstOccurrence(Object o)
1233 return remove(o);
1237 * Removes the last occurrence of the specified element
1238 * from the list, when traversing the list from head to
1239 * tail. The list is unchanged if the element is not found.
1241 * @param o the element to remove.
1242 * @return true if an instance of the object was removed.
1243 * @since 1.6
1245 public boolean removeLastOccurrence(Object o)
1247 Entry<T> e = last;
1248 while (e != null)
1250 if (equals(o, e.data))
1252 removeEntry(e);
1253 return true;
1255 e = e.previous;
1257 return false;