fi.joensuu.cs.tra
Class LinkedDeque<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by java.util.LinkedList<E>
                  extended by fi.joensuu.cs.tra.LinkedDeque<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, Queue<E>

public class LinkedDeque<E>
extends LinkedList<E>

LinkedDeque extends java.util.LinkedList to highlight Deque behaviour. It allows accessing, adding and removing elements elements from both ends of the queue. A simple example where a deque is split and then shuffled back together:

    LinkedDeque 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());
   }

 
This 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.

See Also:
LinkedList, Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
LinkedDeque()
          Creates an empty list.
LinkedDeque(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.
 
Method Summary
 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.
 
Methods inherited from class java.util.LinkedList
add, add, addAll, addAll, clone, contains, element, get, indexOf, lastIndexOf, listIterator, offer, peek, poll, remove, remove, remove, set, size, toArray, toArray
 
Methods inherited from class java.util.AbstractSequentialList
iterator
 
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, retainAll, subList
 

Constructor Detail

LinkedDeque

public LinkedDeque()
Creates an empty list.


LinkedDeque

public LinkedDeque(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.

Parameters:
c - the collection whose elements are to be placed into this deque.
Throws:
NullPointerException - if the specified collection was null.
Method Detail

addFirst

public void addFirst(E o)
Inserts the given element to the front of this deque.

Overrides:
addFirst in class LinkedList<E>
Parameters:
o - the element to be inserted.

addLast

public void addLast(E o)
Inserts the given element to the rear of this deque.

Overrides:
addLast in class LinkedList<E>
Parameters:
o - the element to be inserted.

clear

public void clear()
Removes all the elements from this deque.

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class LinkedList<E>

getFirst

public E getFirst()
Returns but does not remove the element at the front of this deque.

Overrides:
getFirst in class LinkedList<E>
Returns:
the element at the front of this deque or null if this deque is empty.

getLast

public E getLast()
Returns but does not remove the element at the rear of this deque.

Overrides:
getLast in class LinkedList<E>
Returns:
the element at the rear of this deque or null if this deque is empty.

removeFirst

public E removeFirst()
Removes and returns the element at the front of this deque.

Overrides:
removeFirst in class LinkedList<E>
Returns:
the element at the front of this deque or null if this deque is empty.

removeLast

public E removeLast()
Removes and returns the element at the rear of this deque.

Overrides:
removeLast in class LinkedList<E>
Returns:
the element at the rear of this deque or null if this deque is empty.