class Queue: def __init__(self, limit=10): self.data = [None] * limit self.head = -1 self.tail = -1
时间: 2024-05-27 18:14:44 浏览: 111
def is_empty(self): return self.head == -1
def is_full(self): return (self.tail + 1) % len(self.data) == self.head
def enqueue(self, value): if self.is_full(): raise ValueError("Queue is full") if self.is_empty(): self.head = 0 self.tail = (self.tail + 1) % len(self.data) self.data[self.tail] = value
def dequeue(self): if self.is_empty(): raise ValueError("Queue is empty") value = self.data[self.head] if self.head == self.tail: self.head = -1 self.tail = -1 else: self.head = (self.head + 1) % len(self.data) return value
def peek(self): if self.is_empty(): raise ValueError("Queue is empty") return self.data[self.head]
阅读全文