queue

mail@pastecode.io avatar
unknown
python
a year ago
990 B
1
Indexable
Never
class Stack:
    def __init__(self):
        self.stack = []

    def pop(self) -> int:
        return self.stack.pop()

    def size(self) -> int:
        return len(self.stack)

    def append(self, item) -> None:
        self.stack.append(item)


class MyQueue:

    def __init__(self):
        self.master = Stack()
        self.slave = Stack()

    def push(self, x: int) -> None:
        self.master.append(x)

    def pop(self) -> int:
        while self.master.size() != 1:
            self.slave.append(self.master.pop())

        value = self.master.pop()

        while self.slave.size():
            self.master.append(self.slave.pop())

        return value

    def peek(self) -> int:
        while self.master.size():
            value = self.master.pop()
            self.slave.append(value)

        while self.slave.size():
            self.master.append(self.slave.pop())
        
        return value

    def empty(self) -> bool:
        return self.master.size() == 0