class ColaEnlazada
{
    Node top = null;
    Node bottom = null;

    public boolean isEmpty()
    {
        return (top == null);
    }

    public int tamaņo()
    {

        Node actual = top;
        int i;

        for (i = 0; actual != null; i++)
            actual = (Node)actual.getNext();

        return i;
    }

    public Object primero()
    {

        if (top != null)
            return top.getElem();
        else return null;
    }

    public void insertar(Object o)
    {

        if (o != null)
        {
            Node n = new Node();
            n.setElem(o);
            if (bottom != null)
            {
                bottom.setNext(n);
                bottom = n;
            }
            else
                top = bottom = n;
        }
    }

    public Object retirar()
    {

        if (top == null)
            return null;
        Object o = top.getElem();
        top = (Node)top.getNext();
        if (top == null)
            bottom = null;
        return o;
    }

    public void print()
    {
        Node aux = top;
        System.out.print("top--->  ");
        while (aux != null)
        {
            System.out.print("[" + aux.getElem() + "]");
            if(aux.getNext()!=null) System.out.print("<--");

            aux = aux.getNext();
        }
        System.out.println("  <---bottom");
    }

    /* apartado 1 */
    public boolean existeReferencia(Object referencia)
    {
        Node aux = top;
        while (aux != null)
        {
            if (aux.getElem() == referencia)
                return true;
            aux = aux.getNext();
        }
        return false;
    }

    public void insertarPorElPrincipio(Object o)
    {
        Node nuevo = new Node(o, top);
        if(top == null)
        {
            bottom = nuevo;
        }
        top = nuevo;
    }
    
    /* apartado 3 */
    public void eliminarUnoDeCadaDos() {
        Node aux = top;
        while (aux != null) {
            if (aux.getNext() != null) {
                aux.setNext(aux.getNext().getNext());
            }
            bottom = aux;
            aux = aux.getNext();
        }      
    }
}

