Ejemplo n.º 1
0
class PositionalQueue(object):
    """ A queue implementation based on positional list.

    With enqueue returning a position instance and support for a new method,
    delete(p), that removes the element associated with position p from the
    queue.
    """
    def __init__(self):
        self._data = PositionalList()

    def __len__(self):
        """ Length method."""
        return len(self._data)

    def is_empty(self):
        """ check if the queue is empty or not."""
        return len(self._data) == 0

    def first(self):
        """ get the first element of the queue."""
        if self._data.is_empty():
            raise Empty('The queue is empty')
        return self._data.first().element()

    def enqueue(self, e):
        """ add an element to the end of the queue.
        :return: the Position of the element just be enqueued.
        """
        return self._data.add_last(e)

    def dequeue(self):
        """ remove the first element of the queue."""
        self._data.delete(self._data.first())

    def delete(self, p):
        """ Delete the element associated with position p."""
        self._data.delete(p)

    def __repr__(self):
        return str(self._data)