622. Design Circular Queue
https://leetcode.com/problems/design-circular-queue/
Python
class MyCircularQueue:
def __init__(self, k: int):
self.queue = [-1 for _ in range(k)]
self.rear = 0
self.front = 0
self.count = 0
self.cap = k
def enQueue(self, value: int) -> bool:
if self.isFull():
return False
index = (self.front + self.count) % self.cap
self.queue[index] = value
self.count += 1
return True
def deQueue(self) -> bool:
if self.isEmpty():
return False
self.front = (self.front+1) % self.cap
self.count -= 1
return True
def Front(self) -> int:
if self.isEmpty():
return -1
return self.queue[self.front]
def Rear(self) -> int:
if self.isEmpty():
return -1
index = (self.front + self.count - 1) % self.cap
return self.queue[index]
def isEmpty(self) -> bool:
return self.count == 0
def isFull(self) -> bool:
return self.count == self.cap