public class LinkedDeque<E>
extends java.util.LinkedList<E>
LinkedDequeThis should convert a deque of for example 1 2 3 4 5 6 7 8 9 to 9 1 8 2 7 3 6 4 5.mainDeq = new LinkedDeque (); LinkedDeque helpDeq1 = new LinkedDeque (); LinkedDeque helpDeq2 = new LinkedDeque (); // Let's assume there's something in mainDeq
// Split the original deque into two while (!mainDeq.isEmpty()) { // Insert elements to the first helper deque from the top // of the original deque. helpDeq1.addFirst(mainDeq.removeFirst()); // Insert elements to the second helper deque from the bottom if (!mainDeq.isEmpty()) helpDeq2.addFirst(mainDeq.removeLast()); } // Now start filling the original deque by adding elements alternating // between the helper deques while (!helpDeq1.isEmpty()) { mainDeq.addFirst(helpDeq1.removeFirst()); if (!helpDeq2.isEmpty()) mainDeq.addFirst(helpDeq2.removeFirst()); }
LinkedList
,
Serialized FormConstructor and Description |
---|
LinkedDeque()
Creates an empty list.
|
LinkedDeque(java.util.Collection<? extends E> c)
Constructs a deque containing the elements of the specified collection,
in the order they are returned by the collection's iterator.
|
Modifier and Type | Method and Description |
---|---|
void |
addFirst(E o)
Inserts the given element to the front of this deque.
|
void |
addLast(E o)
Inserts the given element to the rear of this deque.
|
void |
clear()
Removes all the elements from this deque.
|
E |
getFirst()
Returns but does not remove the element at the front of this deque.
|
E |
getLast()
Returns but does not remove the element at the rear of this deque.
|
E |
removeFirst()
Removes and returns the element at the front of this deque.
|
E |
removeLast()
Removes and returns the element at the rear of this deque.
|
add, add, addAll, addAll, clone, contains, descendingIterator, element, get, indexOf, lastIndexOf, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, remove, removeFirstOccurrence, removeLastOccurrence, set, size, spliterator, toArray, toArray
containsAll, isEmpty, removeAll, retainAll, toString
public LinkedDeque()
public LinkedDeque(java.util.Collection<? extends E> c)
c
- the collection whose elements are to be placed into this deque.java.lang.NullPointerException
- if the specified collection was null.public void addFirst(E o)
public void addLast(E o)
public void clear()
public E getFirst()
public E getLast()
public E removeFirst()