requirements

Design your loop queue implementation. A circular queue is a linear data structure that operates on a FIFO (first-in, first-out) basis and the tail of the queue is connected behind the head of the queue to form a loop. It is also known as a “ring buffer”.

One of the nice things about a circular queue is that we can use the space that the queue has previously used. In a normal queue, once a queue is full, we cannot insert the next element, even though there is still space in front of the queue. But with circular queues, we can use that space to store new values.

Your implementation should support the following:

  • MyCircularQueue(k): constructor that sets the queue length to K.
  • Front: Retrieves elements from the team leader. If the queue is empty, -1 is returned.
  • Rear: Gets the last element of the team. If the queue is empty, -1 is returned.
  • EnQueue (value): Inserts an element into the loop queue. Return true on successful insertion.
  • DeQueue (): Removes an element from the loop queue. Return true if deleted successfully.
  • IsEmpty (): checks if the loop queue isEmpty.
  • IsFull (): checks if the loop queue isFull.

Example:

MyCircularQueue circularQueue = new MyCircularQueue(3); Circularqueue.enqueue (1); // Set the length to 3 circularqueue.enqueue (1); Circularqueue.enqueue (2); Circularqueue.enqueue (3); Circularqueue.enqueue (4); // Return false, the queue is full circularqueue.rear (); Circularqueue.isfull (); Circularqueue.dequeue (); Circularqueue.enqueue (4); // Return true circularqueue.rear (); / / return 4Copy the code

Tip:

  • All values are in the range 0 to 1000;
  • Operands will be in the range 1 to 1000;
  • Do not use the built-in queue library.

The core code

class MyCircularQueue:
    def __init__(self, k: int) :
        self.queue = [""] * k
        self.max_length = k
        self.start = -1
        self.end = -1

    def enQueue(self, value: int) - >bool:
        if not self.isFull():
            if self.start == -1:
                self.start = 0
            self.end = (self.end + 1) % self.max_length
            self.queue[self.end] = value
            return True
        else:
            return False

    def deQueue(self) - >bool:
        if not self.isEmpty():
            if self.start == self.end:
                self.start,self.end = -1, -1
            else:
                self.start = (self.start + 1) % self.max_length
            return True
        else:
            return False


    def Front(self) - >int:
        return -1 if self.isEmpty() else self.queue[self.start]

    def Rear(self) - >int:
        return -1 if self.isEmpty() else self.queue[self.end]

    def isEmpty(self) - >bool:
        return self.start == -1 and self.end == -1

    def isFull(self) - >bool:
        return (self.end + 1) % self.max_length == self.start

# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()
Copy the code

Above is the implementation of a loop queue implementation process.