Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.3 kB
0
Indexable
Never
class Queue:
    def __init__(self, capacity):
        self.capacity = capacity
        self.queue = [None] * capacity
        self.head = 0
        self.tail = 0
        self.size = 0

    def push(self, x):
        if self.size >= self.capacity:
            raise ValueError("Queue is full")
        self.queue[self.tail] = x
        self.tail = self.tail + 1
        self.size += 1
        if self.tail == self.capacity:
            self.tail = 0

    def pop(self):
        if self.size == 0:
            raise ValueError("Queue is empty")
        self.head += 1
        self.size -= 1
        x = self.queue[self.head - 1]
        self.queue[self.head - 1] = None
        if self.head == self.capacity:
            self.head = 0
        return x

    def clear(self):
        self.queue = [None] * self.capacity
        self.head = 0
        self.tail = 0
        self.size = 0

    def empty(self):
        return self.size == 0

    def Qsize(self):
        return self.size

    def front(self):
        if self.size == 0:
            raise ValueError("Queue is empty")
        return self.queue[self.head]

    def back(self):
        if self.size == 0:
            raise ValueError("Queue is empty")
        return self.queue[self.tail - 1]