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')
     # real item just after header
     return self._header._next._element
 def remove_min(self):
     """
     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
 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')
     # real item just before trailer
     return self._trailer._prev._element
 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')
     # front aligned with head of list
     return self._head._element
예제 #5
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')

        return self._data[self._front]
 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')
     # top of stack is at head of list
     return self._head._element
 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')
     # remove last item from list
     return self._data.pop()
예제 #8
0
 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 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
예제 #10
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')
     # the last item in the list
     return self._data[-1]
 def delete_last(self):
     """
     Remove and return the element from the back of the deque.
     Raise Empty exception if the deque is empty.
     """
     # after header
     if self.is_empty():
         raise Empty('Deque is empty')
     # use inherited method
     return self._delete_node(self._trailer._prev)
 def delete_first(self):
     """
     Remove and return the element from the front of the deque.
     Raise Empty exception if the deque is empty.
     """
     # after header
     if self.is_empty():
         raise Empty('Deque is empty')
     # use inherited method
     return self._delete_node(self._header._next)
 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
     # bypass the former top node
     self._head = self._head._next
     self._size -= 1
     return answer
    def remove_first(self):
        """
        remove the node at the beginning.
        """
        if self.is_empty():
            raise Empty('LinkedList is empty')

        node = self._head
        self._head = self._head._next
        self._size -= 1
        element = node._element
        node = None
        return element
예제 #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')

        answer = self._data[self._front]
        self._data[self._front] = None  # help garbage collection
        self._front = (self._front + 1) % len(self._data)
        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
예제 #18
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.')
     # put minimum item at the end
     self._swap(0, len(self._data) - 1)
     # and remove it from the list
     item = self._data.pop()
     # then fix new root
     self._downheap(0)
     return item._key, item._value
 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
     # special case as queue is empty
     if self.is_empty():
         # removed head had been the tail
         self._tail = None
     return answer
    def remove_node(self, node):
        if self.is_empty():
            raise Empty('LinkedList is empty')

        if self._size == 1:
            self._head = _Node(None, None)
            self._size -= 1
        else:
            if node._next is None:
                self.remove_last()
            else:
                nxt = node._next
                node._element = nxt._element
                node._next = nxt._next
                nxt = None
                self._size -= 1
        return node._element
예제 #21
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
     # removing only element
     if self._size == 1:
         # queue becomes empty
         self._tail = None
     else:
         # bypass the old head
         self._tail._next = oldhead._next
     self._size -= 1
     return oldhead._next
    def remove_last(self):
        """
        remove the node at the end.
        """
        if self.is_empty():
            raise Empty('LinkedList is empty')

        node = self._head
        if self._size == 1:
            self._head = _Node(None, None)
            self._size -= 1
            return node._element

        while node._next is not None:
            node = node._next
            if node._next is None:
                tail = node._next
                self._size -= 1
        node._next = None
        element = tail._element
        element = None
        return element