Circular Queue Data Structure Initialization and Methods Python

PHOTO EMBED

Mon Aug 01 2022 16:14:45 GMT+0000 (UTC)

Saved by @bryantirawan #python #methods #queue #circle #buffer #hack #reactor

class CircularQueue:
    def __init__(self, size):
        self.size = size
        self.head = 0
        self.tail = 0
        self.buffer = [None] * size
        self.length = 0

    def enqueue(self, value):
        self.length += 1
        self.buffer[self.tail] = value
        self.tail += 1
        if self.tail >= self.size:
            self.tail = 0

    def dequeue(self):
        self.length -= 1
        value = self.buffer[self.head]
        self.head += 1
        if self.head >= self.size:
            self.head = 0
        return value
content_copyCOPY

Enqueue/Dequeue big O is O(1) https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-implementation/