def__init__(self, k: int): """ Initialize your data structure here. Set the size of the queue to be k. """ self.queue = [None] * (k + 1) self.maxsize = k + 1 self.front = 0 self.tail = 0
defenQueue(self, value: int) -> bool: """ Insert an element into the circular queue. Return true if the operation is successful. """ if self.isFull(): returnFalse self.queue[self.tail] = value self.tail = (self.tail + 1) % self.maxsize returnTrue
defdeQueue(self) -> bool: """ Delete an element from the circular queue. Return true if the operation is successful. """ if self.isEmpty(): returnFalse self.queue[self.front] = None self.front = (self.front + 1) % self.maxsize returnTrue
defFront(self) -> int: """ Get the front item from the queue. """
if self.isEmpty(): return -1 return self.queue[self.front]
defRear(self) -> int: """ Get the last item from the queue. """ if self.isEmpty(): return -1 return self.queue[self.tail-1]
defisEmpty(self) -> bool: """ Checks whether the circular queue is empty or not. """ return self.front == self.tail
defisFull(self) -> bool: """ Checks whether the circular queue is full or not. """ return (self.tail + 1) % self.maxsize == self.front
if __name__ == '__main__': a =MyCircularQueue(3) print(a.enQueue(1)) print(a.enQueue(2)) print(a.enQueue(3)) print(a.enQueue(4)) print(a.Rear()) print(a.isFull()) print(a.deQueue()) print(a.enQueue(4)) print(a.Rear()) print(a.queue) print(a.Front())