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)
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._tailer._prev)
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)
def dequeue(self): """Remove and return the first element if the queue(i.e., FIFO). Raisee Empty exception if the dueue is empty""" if self.is_empty(): raise Empty("Queue is empty") node = self._data.remove_first() return node.value
def top(self): """ Return (but not remove) at the top of the stack. Raise Empty exception if the stack in empty. Time Complexity: O(1) """ if self.is_empty(): raise Empty("Stack in empty!") return self._data[-1]
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._downheadp(0) return (item._key, item._value)
def first(self): """Return the first value in the list. Or raise Empty error if the list is empty. Time complexity: O(1) """ if self._size == 0: raise Empty('The list is empty!') return self._header._next._element
def last(self): """Return the last value in the list. Or raise Empty error if the list is empty. Time complexity: O(1) """ if self._size == 0: raise Empty('The list is empty!') return self._tailer._prev._element
def pop(self): """ Remove and return the element from the top of the stack(LIFO) Raise Empty exception if the stack is empty. Time Complexity: O(1)* """ if self.is_empty(): raise Empty("Stack is empty!") return self._data.pop()
def first(self): """ Return the first element. Raise Empty Error if the queue is empty Time complexity: O(1) """ if self.is_empty(): raise Empty("Queue is empty!") return self._data[self._font]
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 remove_last(self): """Remove the last node of the list Time complexity: O(1)""" if self.is_empty(): raise Empty("The list is empty!") element = self.first() self._tailer._prev._prev = self._tailer self._tailer._prev = self._tailer._prev._prev self._size -= 1 return element
def pop(self): """ Remove and return the element from the top of the stack(LIFO) Raise Empty exception if the stack is empty. Time Complexity: O(1) """ if self.is_empty(): raise Empty("Stack is empty!") ele = self._data.tail.value self._data.remove_last() return ele
def remove_first(self): """ Remove the first element in the single linked list. Or raise an empty error when there is no node in the list. Time complexity: O(1) """ if self.is_empty(): raise Empty("The single linked list is empty") answer = self.head self.head = self.head.next self.size -= 1 return answer
def remove_first(self): """ Remove the first node of the list. Or raise an Empty error if the list is empty. Time complexity: O(1) """ if self.is_empty(): raise Empty("The list is empty!") element = self.first() self._header._next._next._prev = self._header self._header._next = self._header._next._next self._size -= 1 return element
def delete_last(self): """ Remove and return the last element from deque. """ if self.is_empty(): raise Empty('Dequeue is empty!') answer = self.last() back = (self._font + self._size - 1) % len(self._data) self._data[back] = None self._size -= 1 # shrinks if 0 < self._size < len(self._data) // 4: self._resize(len(self._data) // 2) return answer
def dequeue(self): """ Remove and return the first element. Raise Empty error if queue is empty Time complexity: O(1)* Note that `*` means amortized. """ if self.is_empty(): raise Empty("Queue is empty!") answer = self._data[self._font] # help garbage collection self._data[self._font] = None self._font = (self._font + 1) % len(self._data) self._size -= 1 # shrinks if 0 < self._size < len(self._data) // 4: self._resize(len(self._data) // 2) return answer
def remove_last(self): """ Remove the last element in the single linked list. Or raise an empty error when there is no node in the list. Time complexity: O(n) """ if self.is_empty(): raise Empty("The single linked list is empty") answer = self.tail # when head = tail, we should change head too. if self.size == 1: self.head = None self.tail = None else: pointer = self.head while pointer.next != self.tail: pointer = pointer.next self.tail = pointer self.tail.next = None self.size -= 1 return answer
def first(self): """Return(but not remove) the element at the front of the queue """ if self.is_empty(): raise Empty("Deque is empty!") 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 min(self): if self.is_empty(): raise Empty('Priority queue is empty') item = self._data[0] return (item._key, item._value)
def first(self): """Return(but do not remove) the element at the front if the queue""" if self.is_empty(): raise Empty("Queue is empty") return self._data.head.value
def last(self): """Return(but not remove) the element at the back of the deque """ if self.is_empty(): raise Empty("Deque is empty!") return self._tailer._prev._element