
/**
 *    Implementation of a Deque. This version uses either a linked list or an array list
 *    as the base for constructing a deque.  That is, we are using a more general data
 *    structure (a list) to make a restricted data structure (a deque).
 *
 */
public class Deque implements DequeADT
{
    private LinkedList list;
    
    public Deque()
    {
        list = new LinkedList();
    }

    public Object pushFront( Object newEntry )
    {
        try
        {
            list.append(newEntry);
        }
        catch( NullPointerException e ) {}
        return newEntry;
    }

    public Object pushBack( Object newEntry )
    {
        try
        {
            list.add(0,newEntry);
        }
        catch( NullPointerException e ) {}
        catch( ListLocationException e ) {}
        return newEntry;
    }

    public Object popFront()
    {
        Object popped = null;
        try
        {
            popped = list.remove(list.size-1);
        }
        catch( ListLocationException e ) {}
        return popped;
    }

    public Object popBack()
    {
        Object popped = null;
        try
        {
            popped = list.remove(0);
        }
        catch( ListLocationException e ) {}
        return popped;
    }

    public Object front()
    {
        Object front = null;
        try
        {
            front = list.get(list.size-1);
        }
        catch( ListLocationException e ) {}
        return front;
    }

    public Object back()
    {
        Object back = null;
        try
        {
            back = list.get(0);
        }
        catch( ListLocationException e ) {}
        return back;
    }

    public boolean isEmpty()
    {
        return list.isEmpty();
    }

    public void clear()
    {
        list.clear();
    }
} // End class Deque


