跳到主要内容

Design Circular Queue

描述

TODO

分析

TODO

代码

# Design Circular Queues
class MyCircularQueue:
def __init__(self, k: int):
self.q = [0] * k
self.capacity = k
self.head = 0
self.count = 0

def enQueue(self, value: int) -> bool:
if self.isFull(): return False
self.q[(self.head + self.count) % self.capacity] = value
self.count += 1
return True

def deQueue(self) -> bool:
if self.isEmpty(): return False
self.head = (self.head + 1) % self.capacity
self.count -= 1
return True

def Front(self) -> int:
if self.isEmpty(): return -1
return self.q[self.head]

def Rear(self) -> int:
if self.isEmpty(): return -1
tail = (self.head + self.count - 1) % self.capacity
return self.q[tail]

def isEmpty(self) -> bool:
return self.count == 0

def isFull(self) -> bool:
return self.count == self.capacity