- make [k+1] as maximum space
- terms: front,rear
Reference
https://www.youtube.com/watch?v=ihEmEcO2Hx8
https://mailmail.tistory.com/41
https://reakwon.tistory.com/30
class MyCircularQueue:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the queue to be k.
"""
self.queue = (k+1) * [None]
self.front = 0
self.rear = 0
self.k = k + 1
def next_index(self):
pass
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
else:
self.rear = (self.rear + 1) % self.k
self.queue[self.rear] = value
print("enqueue", value, self.front, self.rear)
return True
def deQueue(self) -> bool:
"""
Delete an element from the circular queue. Return true if the operation is successful.
"""
if self.front == self.rear:
return False
else:
self.queue[self.front] = None
self.front = (self.front + 1) % self.k
return True
def Front(self) -> int:
"""
Get the front item from the queue.
"""
return self.queue[self.front]
def Rear(self) -> int:
"""
Get the last item from the queue.
"""
return self.queue[self.rear]
def isEmpty(self) -> bool:
"""
Checks whether the circular queue is empty or not.
"""
if self.rear == self.front:
return True
def isFull(self) -> bool:
"""
Checks whether the circular queue is full or not.
"""
return (self.rear + 1) % self.k == self.front