622 - Design Circular Queue
    Written on January 25, 2020
    
    
    
    
    
    Tweet
  Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called “Ring Buffer”. Your implementation should support following operations:
- MyCircularQueue(k): Constructor, set the size of the queue to be k.
- Front: Get the front item from the queue. If the queue is empty, return -1.
- Rear: Get the last item from the queue. If the queue is empty, return -1.
- enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
- deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
- isEmpty(): Checks whether the circular queue is empty or not.
- isFull(): Checks whether the circular queue is full or not.
class MyCircularQueue:
    def __init__(self, k: int):
        """
        Initialize your data structure here. Set the size of the queue to be k.
        """
        self.size = 0
        self.max_size = k
        self.first = self.last = -1
        self.queue = [0] * k
    def enQueue(self, value: int) -> bool:
        """
        Insert an element into the circular queue. Return true if the operation is successful.
        """
        if self.size == self.max_size:
            return False
        if self.last == -1:
            self.first = self.last = 0
        else:
            self.last = (self.last + 1) % self.max_size
        self.queue[self.last] = value
        self.size += 1
        return True
    def deQueue(self) -> bool:
        """
        Delete an element from the circular queue. Return true if the operation is successful.
        """
        if self.size == 0:
            return False
        if self.first == self.last:
            self.first = self.last = -1
        else:
            self.first = (self.first + 1) % self.max_size
        self.size -= 1
        return True
    def Front(self) -> int:
        """
        Get the front item from the queue.
        """
        return self.queue[self.first] if self.first != -1 else -1
    def Rear(self) -> int:
        """
        Get the last item from the queue.
        """
        return self.queue[self.last] if self.last != -1 else -1
    def isEmpty(self) -> bool:
        """
        Checks whether the circular queue is empty or not.
        """
        return self.size == 0
    def isFull(self) -> bool:
        """
        Checks whether the circular queue is full or not.
        """
        return self.size == self.max_size
