Ejemplo n.º 1
0
    def pop(self):
        """Remove and return the element from teh top of the stack.

        Raise Empty exception if the stack is empty."""
        if self.is_empty():
            raise Empty("Can't pop from an empty stack.")
        return self._data.pop()
Ejemplo n.º 2
0
 def min(self):  # O(1)
     """Return but don't remove (k, v) tuple with minimum key."""
     if self.is_empty():
         raise Empty("Priority queue is empty.")
     pos = self._data.first()
     item = pos.element()
     return (item._key, item._value)
Ejemplo n.º 3
0
    def first(self):
        """Return the first element of the deque.

        Raise Empty exception if the deque is empty.
        """
        if self.is_empty():
            raise Empty("Deque is empty.")
        return self._data[self._front]
Ejemplo n.º 4
0
    def last(self):
        """Return the element at the back of the queue.

        Raise Empty exception if the queue is empty.
        """
        if self.is_empty():
            raise Empty("Queue is empty.")
        return self._tail._element
Ejemplo n.º 5
0
    def delete_last(self):
        """Remove and return the element from the back of the deque.

        Raise Empty exception if the deque is empty.
        """
        if self.is_empty():
            raise Empty("Deque is empty.")
        return self._delete_node(self._trailer._prev)
Ejemplo n.º 6
0
    def delete_first(self):
        """Remove and return the element from the front of the deque.

        Raise Empty exception if the deque is empty.
        """
        if self.is_empty():
            raise Empty("Deque is empty.")
        return self._delete_node(self._header._next)
Ejemplo n.º 7
0
 def pop(self):
     """Remove and return the element from the top of the stack."""
     if self.is_empty():
         raise Empty("Stack is empty.")
     value = self._top._element
     self._top = self._top._next
     self._size -= 1
     return value
Ejemplo n.º 8
0
    def min(self):
        """Return but don't remove (k, v) tuple with minimum key.

        Raise Empty exception if empty.
        """
        if self.is_empty():
            raise Empty("Priority queue is empty.")
        item = self._data[0]
        return (item._key, item._value)
Ejemplo n.º 9
0
    def first(self):
        """Return the element at the front of the queue.

        Raise Empty exception if the queue is empty.
        """
        if self.is_empty():
            raise Empty("Queue is empty.")
        head = self._tail._next
        return head._element
Ejemplo n.º 10
0
    def last(self):
        """Return the last element of the deque.

        Raise Empty exception if the deque is empty.
        """
        if self.is_empty():
            raise Empty("Deque is empty.")
        last = (self._front + self._size - 1) % len(self._data)
        return self._data[last]
Ejemplo n.º 11
0
 def delete_last(self):
     """Remove and return the last element from the deque."""
     if self.is_empty():
         raise Empty("Queue is empty.")
     last = (self._front + self._size - 1) % len(self._data)
     answer = self._data[last]
     self._data[last] = None
     self._size -= 1
     if self._size < len(self._data) // 4:
         self._resize(len(self._data) // 2)
     return answer
Ejemplo n.º 12
0
 def _find_min(self):  # O(n)
     """Return Position of item with minimum key."""
     if self.is_empty():
         raise Empty("Priority queue is empty.")
     small = self._data.first()
     walk = self._data.after(small)
     while walk is not None:
         if walk.element() < small.element():
             small = walk
         walk = self._data.after(walk)
     return small
Ejemplo n.º 13
0
    def remove_min(self):
        """Remove and return (k, v) tuple with minimum key.

        Raise Empty exception if empty.
        """
        if self.is_empty():
            raise Empty("Priority queue is empty.")
        self._swap(0, len(self._data) - 1)
        item = self._data.pop()
        self._downheap(0)
        return (item._key, item._value)
Ejemplo n.º 14
0
    def dequeue(self):
        """Remove and return the first element of the queue. (i.e., FIFO.)

        Raise Empty exception if the queue is empty.
        """
        if self.is_empty():
            raise Empty("Queue is empty.")
        value = self._head._element
        self._head = self._head._next
        self._size -= 1
        if self.is_empty():
            self._tail = None
        return value
Ejemplo n.º 15
0
    def dequeue(self):
        """Remove and return the first element of the queue (i.e., FIFO).

        Raise Empty exception if the queue is empty.
        """
        if self.is_empty():
            raise Empty("Queue is empty.")
        answer = self._data[self._front]
        self._data[self._front] = None
        self._front = (self._front + 1) % len(self._data)
        self._size -= 1
        if self._size < len(self._data) // 4:
            self._resize(len(self._data) // 2)
        return answer
Ejemplo n.º 16
0
    def dequeue(self):
        """Remove and return the first element of the queue. (i.e., FIFO)

        Raise Empty exception if the queue is empty.
        """
        if self.is_empty():
            raise Empty("Queue is empty.")
        oldhead = self._tail._next
        if self._size == 1:
            self._tail = None
        else:
            self._tail._next = oldhead._next
        self._size -= 1
        return oldhead._element
Ejemplo n.º 17
0
 def last(self):
     """Return the element at the end of the deque."""
     if self.is_empty():
         raise Empty("Deque is empty.")
     return self._trailer._prev._element
Ejemplo n.º 18
0
 def first(self):
     """Return the element at the front of the deque."""
     if self.is_empty():
         raise Empty("Deque is empty.")
     return self._header._next._element
Ejemplo n.º 19
0
 def remove_min(self):  # O(1)
     """Remove and return (k, v) tuple with minimum key."""
     if self.is_empty():
         raise Empty("Priority queue is empty.")
     item = self._data.delete(self._data.first())
     return (item._key, item._value)
Ejemplo n.º 20
0
 def first(self):
     """Return the element at the front of the queue"""
     if self.is_empty():
         raise Empty("Queue is empty.")
     return self._head._element
Ejemplo n.º 21
0
 def peek(self):
     """Return the element at the top of the stack."""
     if self.is_empty():
         raise Empty("Can't peek at an empty stack.")
     return self._data[-1]
Ejemplo n.º 22
0
 def peek(self):
     """Return the element at the top of the stack."""
     if self.is_empty():
         raise Empty("Stack is empty.")
     return self._top._element