Exemple #1
0
    def first(self):
        """Return a reference to the element at the front of the deque.

        Raise Empty exception if the deque is empty."""
        if self.is_empty():
            raise Empty('The deque is empty.')
        return self._data[self._front]
Exemple #2
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)
Exemple #3
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)
 def min(self):
     """Return but do not remove (k, v) tuple with minimum key."""
     if self.is_empty():
         raise Empty('Priority queue is empty.')
     p = self._data.first()
     item = p.element()
     return (item._key, item._value)
Exemple #5
0
    def first(self):
        """
        Returns the lement at the front of the queue, without removing it.

        Raises Empty exception if the queue is empty."""
        if self.is_empty():
            raise Empty('The queue is empty')
        return self._data[self._front]
Exemple #6
0
    def first(self):
        """Return (but do not remove) 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
    def top(self):
        """Return (but do not remove) the element at the top of the stack.

        Raise Empty exception if the stack is empty.
        """
        if self.is_empty():
            raise Empty('Stack is empty')
        return self._head._element  # top of stack is at head of list
 def pop(self):
     """
     Removes and returns the element from the top of the stack.
     Raises Empty exception if the stack is empty.
     """
     if self.is_empty():
         raise Empty('The stack is empty')
     return self._data.pop()
 def top(self):
     """
     Returns the element at the top of the stack, without removing it.
     Raises Empty exception if the stack is empty.
     """
     if self.is_empty():
         raise Empty('The stack is empty')
     return self._data[-1]
Exemple #10
0
    def last(self):
        """Return a reference to the element at the back of the deque.

        Raise Empty exception if the deque is empty."""
        if self.is_empty():
            raise Empty('The deque is empty')
        last_in_line = (self._front + self._size - 1) % len(self._data)
        return self._data[last_in_line]
    def min(self):
        """Return but do not 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)
    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)  # put minimum item at the end
        item = self._data.pop()  # remove the minimum item from the list
        self._downheap(0)  # fix the new root
        return (item._key, item._value)
    def pop(self):
        """Remove and return the element from the top of the stack (i.e., LIFO).

        Raise Empty exception if the stack is empty.
        """
        if self.is_empty():
            raise Empty('Stack is empty')
        answer = self._head._element
        self._head = self._head._next
        self._size -= 1
        return answer
 def _find_min(self):
     """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
    def pop(self):
        """Remove and return the last element in the array.

        Raise Empty exception if the array is empty."""
        if self._n == 0:
            raise Empty('The array is empty')
        answer = self._A[self._n - 1]
        self._A[self._n - 1] = None  # help garbage collection
        self._n -= 1
        if self._capacity // 4 > self._n:  # shrink capacity if needed
            self._resize(self._capacity // 2)
        return answer
Exemple #16
0
    def delete_last(self):
        """Delete an element from the back of the deque.

        Raise Empty exception if the deque is empty."""
        if self.is_empty():
            raise Empty('The deque is empty.')

        last_in_line = (self._front + self._size - 1) % len(self._data)
        result = self._data[last_in_line]
        self._data[last_in_line] = None
        self._size -= 1

        return result
Exemple #17
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._head._element
        self._head = self._head._next
        self._size -= 1
        if self.is_empty():
            self._tail = None
        return answer
Exemple #18
0
    def delete_first(self):
        """Delete an element from the front of the deque.

        Raise Empty exception if the deque is empty."""
        if self.is_empty():
            raise Empty('The deque is empty.')

        result = self._data[self._front]  # Save result to return later
        self._data[self._front] = None
        self._front = (self._front + 1) % len(self._data)
        self._size -= 1

        return result
Exemple #19
0
    def dequeue(self):
        """
        Removes and returns the element in front of the queue.

        Raises Empty exception if the queue is empty."""
        if self.is_empty():
            raise Empty('The queue is empty')

        result = self._data[self._front]
        self._data[self._front] = None
        self._front = (self._front + 1) % len(self._data)
        self._size -= 1

        return result
Exemple #20
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
Exemple #21
0
 def first(self):
     """Return (but do not remove) the element at the front of the queue."""
     if self.is_empty():
         raise Empty('Queue is empty')
     return self._head._element
 def remove_min(self):
     """Remove and return (k, v) tuple with minimum key from the priority queue."""
     if self.is_empty():
         raise Empty('Priority queue is empty.')
     item = self._data.delete(self._data.first())
     return (item._key, item._value)
Exemple #23
0
 def last(self):
     """Return (but do not remove) the element at the back of the deque."""
     if self.is_empty():
         raise Empty('Deque is empty')
     return self._trailer._prev._element  # the item before the trailer sentinel
Exemple #24
0
 def first(self):
     """Return (but do not remove) the element at the front of the deque."""
     if self.is_empty():
         raise Empty('Deque is empty')
     return self._header._next._element  # the item after the header sentinel