class MyCircularQueue: def __init__(self, k: int): """ Initialize your data structure here. Set the size of the queue to be k. """ self.k=k self.front=-1 self.rear=-1 self.queue=[None]*k def enQueue(self, value: int) -> bool: """ Insert an element into the circular queue. Return true if the operation is successful. """ if self.isFull(): return False if self.isEmpty(): self.front = 0 self.rear = (self.rear + 1) % self.k self.queue[self.rear] = value return True def deQueue(self) -> bool: """ Delete an element from the circular queue. Return true if the operation is successful. """ if self.isEmpty(): return False if self.front == self.rear: # 出栈不需要真的删除元素,只需要改变头尾指针 self.front = -1 # 首尾指针指到相同元素,且不为-1,此时必然仅有一个元素 self.rear = -1 return True self.front = (self.front + 1) % self.k return True def Front(self) -> int: """ Get the front item from the queue. """ return -1 if self.isEmpty() else self.queue[self.front] def Rear(self) -> int: """ Get the last item from the queue. """ return -1 if self.isEmpty() else self.queue[self.rear] def isEmpty(self) -> bool: """ Checks whether the circular queue is empty or not. """ return self.front == -1 and self.rear==-1 def isFull(self) -> bool: """ Checks whether the circular queue is full or not. """ return (self.rear+1)%self.k==self.front # 输出队列中的元素 def ShowQueue(self): for i in range(self.k): print(self.queue[i],end=' ') print(' ')