def last(self): if self.is_empty(): raise EmptyError('Queue is empty') # 最后一个数据保的位置, 比队尾空位置提前一个索引 last_idx = (self._front + self._size - 1) % self.capacity return self._data[last_idx]
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 EmptyError('Queue is empty') return self._data[self._front]
def pop(self): if self.is_empty(): raise EmptyError('Stack is empty') item = self._top._element self._top = self._top._next self._size -= 1 return item
def dequeue(self): if self.is_empty(): raise EmptyError('Stack is empty') item = self._front._element self._front = self._front._next self._size -= 1 if self.is_empty(): self._back = None return item
def dequeue(self): if self.is_empty(): raise EmptyError('Queue is empty') item = self._data[self._front] self._data[self._front] = None self._front = (self._front + 1) % self.capacity self._size -= 1 if 0 < self._size < self.capacity // 4: self._resize(self.capacity // 2) return item
def dequeue(self): if self.is_empty(): raise EmptyError('Stack is empty') old_head = self._tail._next if self._size == 1: self._tail = None else: self._tail._next = old_head._next self._size -= 1 return old_head._element
def delete_last(self): if self.is_empty(): raise EmptyError('Queue is empty') last_idx = (self._front + self._size - 1) % self.capacity item = self._data[last_idx] self._data[last_idx] = None self._size -= 1 if 0 < self._size < self.capacity // 4: self._resize(self.capacity // 2) return item
def first(self): if self.is_empty(): raise EmptyError('Stack is empty') return self._front._element
def first(self): if self.is_empty(): raise EmptyError("Deque is empty") return self._header._next._element
def delete_last(self): if self.is_empty(): raise EmptyError("Deque is empty") return self._delete_node(self._trailer._prev)
def delete_first(self): if self.is_empty(): raise EmptyError("Deque is empty") return self._delete_node(self._header._next)
def last(self): if self.is_empty(): raise EmptyError("Deque is empty") return self._trailer._prev._element
def top(self): if self.is_empty(): raise EmptyError('Stack is empty') return self._top._element
def top(self): if self.is_empty(): raise EmptyError('Stack is empty.') return self._data[-1]