Class Deque

java.lang.Object
  |
  +--Deque

public class Deque
extends java.lang.Object

A "deque" is a double-ended queue: it is a data structure which restricts access to ends. This particular deque class is capable of only storing doubles. Each Deque has a "capacity" which is the number of elements the Deque can hold before the Deque is automatically expanded.

When a deque is first created, it is empty.


Constructor Summary
Deque()
          Creates an empty Deque
Deque(int capacity)
          Creates an empty deque with the specified initial capacity
 
Method Summary
 void addBack(double element)
          Adds the specified element to the back of this Deque.
 void addFront(double element)
          Adds the specified element to the front of this Deque.
 boolean isEmpty()
          Returns true if the there are no elements in this Deque, false otherwise
 double peekBack()
          Returns the element at the back of this Deque.
 double peekFront()
          Returns the element at the front of this Deque.
 double removeBack()
          Removes and returns the element at the back of this Deque.
 double removeFront()
          Removes and returns the element at the front of this Deque.
 int size()
          Returns the number of elements currently in this Deque
 java.lang.String toString()
          Returns a String representation of this Deque from front to back.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Deque

public Deque()
Creates an empty Deque

Deque

public Deque(int capacity)
Creates an empty deque with the specified initial capacity
Parameters:
capacity - the initial capacity of this Deque
Method Detail

addFront

public void addFront(double element)
Adds the specified element to the front of this Deque. The Deque is expanded if necessary
Parameters:
element - the element to tadd ot this Deque

addBack

public void addBack(double element)
Adds the specified element to the back of this Deque. The Deque is expanded if necessary
Parameters:
element - the element to tadd ot this Deque

peekFront

public double peekFront()
Returns the element at the front of this Deque. Note the element is not removed. If this Deque is empty, a NoSuchElementException is issued
Returns:
the element at the front of this Deque
Throws:
java.util.NoSuchElementException - if this Deque is empty

peekBack

public double peekBack()
Returns the element at the back of this Deque. Note the element is not removed. If this Deque is empty, a NoSuchElementException is issued
Returns:
the element at the back of this Deque
Throws:
java.util.NoSuchElementException - if this Deque is empty

removeFront

public double removeFront()
Removes and returns the element at the front of this Deque. If this Deque is empty, a NoSuchElementException is issued
Returns:
the element at the front of this Deque
Throws:
java.util.NoSuchElementException - if this Deque is empty

removeBack

public double removeBack()
Removes and returns the element at the back of this Deque. If this Deque is empty, a NoSuchElementException is issued
Returns:
the element at the back of this Deque
Throws:
java.util.NoSuchElementException - if this Deque is empty

toString

public java.lang.String toString()
Returns a String representation of this Deque from front to back. An example of actions on a deque are shown below, along with what the toString() method should return after each operation:

     Deque dq = new Deque();  // toString() returns "[]"
     dq.addFront(0.2);        // toString() returns "[0.2]"
     dq.addBack(0.1);         // toString() returns "[0.2, 0.1]"
     dq.addFront(0.3);        // toString() returns "[0.3, 0.2, 0.1]"
 
Returns:
a String representation of this Deque
Overrides:
toString in class java.lang.Object

size

public int size()
Returns the number of elements currently in this Deque
Returns:
the number of elements currently in this Deque

isEmpty

public boolean isEmpty()
Returns true if the there are no elements in this Deque, false otherwise
Returns:
true if there are no elements in this Deque, false otherwise