@SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object that) {
        if (!(that instanceof PDeque))
            return false;
        PDeque queue = (PDeque) that;

        if (size() != queue.size())
            return false;

        if (size() == 0) {
            return queue.size() == 0;
        } else {
            Object obj1 = peek();
            Object obj2 = queue.peek();
            return (obj1 == null ? obj2 == null : obj1.equals(obj2)) && tail().equals(queue.tail());
        }
    }