--- jsr166/src/main/java/util/LinkedList.java 2004/11/21 01:40:39 1.16 +++ jsr166/src/main/java/util/LinkedList.java 2004/12/28 12:14:07 1.17 @@ -5,7 +5,7 @@ * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ -package java.util; +package java.util; /** * Linked list implementation of the List interface. Implements all @@ -14,14 +14,11 @@ package java.util; * the LinkedList class provides uniformly named methods to * get, remove and insert an element at the * beginning and end of the list. These operations allow linked lists to be - * used as a stack, queue, or double-ended queue (deque).

+ * used as a stack, queue, or double-ended queue ({@link Deque}).

* - * The class implements the Queue interface, providing + * The class implements the Deque interface, providing * first-in-first-out queue operations for add, - * poll, etc. Other stack and deque operations could be - * easily recast in terms of the standard list operations. They're - * included here primarily for convenience, though they may run - * slightly faster than the equivalent List operations.

+ * poll, along with other stack and deque operations. * * All of the operations perform as could be expected for a doubly-linked * list. Operations that index into the list will traverse the list from @@ -73,7 +70,7 @@ package java.util; public class LinkedList extends AbstractSequentialList - implements List, Queue, Cloneable, java.io.Serializable + implements List, Deque, Cloneable, java.io.Serializable { private transient Entry header = new Entry(null, null, null); private transient int size = 0; @@ -497,6 +494,156 @@ public class LinkedList return add(o); } + // Deque operations + /** + * Inserts the specified element to the front this deque. + * + * @param e the element to insert + * @return true (as per the spec for {@link Deque#offerFirst}) + * @since 1.6 + */ + public boolean offerFirst(E e) { + addFirst(e); + return true; + } + + /** + * Inserts the specified element to the end this deque. + * + * @param e the element to insert + * @return true (as per the spec for {@link Deque#offerLast}) + * @since 1.6 + */ + public boolean offerLast(E e) { + addLast(e); + return true; + } + + /** + * Retrieves, but does not remove, the first element of this deque, + * returning null if this deque is empty. + * + * @return the first element of this deque, or null if + * this deque is empty + * @since 1.6 + */ + public E peekFirst() { + if (size==0) + return null; + return removeFirst(); + } + + /** + * Retrieves, but does not remove, the last element of this deque, + * returning null if this deque is empty. + * + * @return the last element of this deque, or null if this deque + * is empty + * @since 1.6 + */ + public E peekLast() { + if (size==0) + return null; + return removeLast(); + } + + /** + * Retrieves and removes the first element of this deque, or + * null if this deque is empty. + * + * @return the first element of this deque, or null if + * this deque is empty + * @since 1.6 + */ + public E pollFirst() { + if (size==0) + return null; + return removeFirst(); + } + + /** + * Retrieves and removes the last element of this deque, or + * null if this deque is empty. + * + * @return the last element of this deque, or null if + * this deque is empty + * @since 1.6 + */ + public E pollLast() { + if (size==0) + return null; + return removeLast(); + } + + /** + * Pushes an element onto the stack represented by this deque. In other + * words, inserts the element to the front this deque. + * + *

This method is equivalent to {@link #addFirst}. + * + * @param e the element to push + * @since 1.6 + */ + public void push(E e) { + addFirst(e); + } + + /** + * Pops an element from the stack represented by this deque. In other + * words, removes and returns the the first element of this deque. + * + *

This method is equivalent to {@link #removeFirst()}. + * + * @return the element at the front of this deque (which is the top + * of the stack represented by this deque) + * @throws NoSuchElementException if this deque is empty + * @since 1.6 + */ + public E pop() { + return removeFirst(); + } + + /** + * Removes the first occurrence of the specified element in this + * deque (when traversing the deque from head to tail). If the deque + * does not contain the element, it is unchanged. + * + * @param e element to be removed from this deque, if present + * @return true if the deque contained the specified element + * @since 1.6 + */ + public boolean removeFirstOccurrence(Object e) { + return remove(e); + } + + /** + * Removes the last occurrence of the specified element in this + * deque (when traversing the deque from head to tail). If the deque + * does not contain the element, it is unchanged. + * + * @param o element to be removed from this deque, if present + * @return true if the deque contained the specified element + * @since 1.6 + */ + public boolean removeLastOccurrence(Object o) { + if (o==null) { + for (Entry e = header.previous; e != header; e = e.previous) { + if (e.element==null) { + remove(e); + return true; + } + } + } else { + for (Entry e = header.previous; e != header; e = e.previous) { + if (o.equals(e.element)) { + remove(e); + return true; + } + } + } + return false; + } + /** * Returns a list-iterator of the elements in this list (in proper * sequence), starting at the specified position in the list.