public class NodeList<E> implements List<E>
{
    private int count;
    private DNode<E> head, tail;
    
    public NodeList()
    {
        count = 0;
        head = new DNode<E>( null, null, null );
        tail = new DNode<E>( head, null, null );
        head.setNext( tail );
    }
    
    public int size(){ return count; }
    public boolean isEmpty(){ return count == 0; }
    
    public Position<E> first(){ if ( count == 0 )
                                    return null;
                                return head.getNext(); }
                                
    public Position<E> last(){ return null; }
    
    public Position<E> next( Position<E> p ) throws InvalidPositionException,
        BoundaryViolationException
    { 
        DNode<E> current = checkPosition( p );
        
        if ( current.getNext() == tail )
            throw new BoundaryViolationException(" Can't go past end of list ");
        
        return current.getNext();
    }
    public Position<E> prev( Position<E> p ) throws InvalidPositionException,
        BoundaryViolationException{ return null; }
    public Position<E> insertFirst( E elem )
    { 
        head.getNext().setPrev( new DNode<E>( head, head.getNext(), elem ) );
        head.setNext( head.getNext().getPrev() );
        count++;
        return head.getNext();
    }
    public Position<E> insertLast( E elem ){ return null; }
    public Position<E> insertAfter( Position<E> p, E elem ) throws InvalidPositionException{ return null; }
    public Position<E> insertBefore( Position<E> p, E elem ) throws InvalidPositionException{ return null; }
    public E remove( Position<E> p ) throws InvalidPositionException
    { 
        DNode<E> remNode = checkPosition( p );
        
        remNode.getPrev().setNext( remNode.getNext() );
        remNode.getNext().setPrev( remNode.getPrev() );
        
        count--;
        
        return remNode.element(); 
    }
    public E replace( Position<E> p, E elem ) throws InvalidPositionException{ return null; }  
    
    private DNode<E> checkPosition( Position<E> p ) throws InvalidPositionException
    {
        if ( p == null )
            throw new InvalidPositionException(" Null is not a valid position ");
        if ( p == head )
            throw new InvalidPositionException(" The header node is not a valid position ");
        if ( p == tail )
            throw new InvalidPositionException(" The trailer node is not a valid position ");
        try
        {
            DNode<E> temp = (DNode<E>)p;
            if ( temp.getPrev() == null || temp.getNext() == null )
                throw new InvalidPositionException(" Not a valid position in a list ");
            return temp;
        }
        catch( ClassCastException e )
        {
            throw new InvalidPositionException(" The Position is of the wrong type ");
        }
    }
}
