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.Empty('Deque is empty') return self._delete_node(self._trailer._prev)
def pop(self): """Remove and return the element from the top of the stack""" if self.is_empty(): raise empty.Empty('Stack is empty') answer = self._head._element self._head = self._head._next self._size -= 1 return answer
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.Empty("Queue is empty") return self._data[self._front]
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.Empty('Deque is empty') return self._delete_node(self._header._next)
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.Empty('Stack is empty') return self._data[-1]
def pop(self): '''Return and remove the element at the top of the stack. Raise Empty exception if the stack is empty. ''' if self.is_empty(): raise empty.Empty('Stack is empty') print("Removed {} from the stack!".format(self.top())) if self.top() == self._min_stack[-1]: self._min_stack.pop() return self._data.pop()
def dequeue(self): '''Remove and return the first element of the queue. Raise Empty exception if the queue is empty. ''' if self.is_empty(): raise empty.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 if 0 < self._size < len(self._data) // 4: self._resize(len(self._data) // 2) # shrinking the underlying array return answer
def top(self): """Return (but do not remove) the element at the top of the stack""" if self.is_empty(): raise empty.Empty('Stack is empty') return self._head._element
def first(self): '''Return (but not remove) an element at the front of the deque''' if self.is_empty(): raise empty.Empty('Deque is empty') return self._header._next._element
def last(self): '''Return (but not remove) an element at the back of the deque''' if self.is_empty(): raise empty.Empty('Deque is empty') return self._trailer._prev._element
def min(self): if not self._min_stack: raise empty.Empty('Stack is empty') return self._min_stack[-1]