def reverse(self):
        if self.queue.isEmpty():
            return

        reversedStack = Stack()
        for i in range(self.queue.size):
            reversedStack.push(self.queue.pop().data)

        self.queue = reversedStack
        self.isReversed = not self.isReversed
Пример #2
0
    def push(self, newData):
        if self.isCapacityFull():
            # add new stack
            newStack = Stack()
            newStack.push(newData)
            self.set.append(newStack)
            self.singleStackCapacity += 1
            print("New stack is created")
            print("New capacity is " + str(self.singleStackCapacity))

        else:
            lastStackIndex = len(self.set) - 1
            self.set[lastStackIndex].push(newData)
class MyQueue:
    def __init__(self):
        self.queue = Stack()
        self.isReversed = False

    def reverse(self):
        if self.queue.isEmpty():
            return

        reversedStack = Stack()
        for i in range(self.queue.size):
            reversedStack.push(self.queue.pop().data)

        self.queue = reversedStack
        self.isReversed = not self.isReversed

    def dequeue(self):
        if not self.isReversed:
            # need to reverse to pop the first element
            self.reverse()

        if self.queue.isEmpty():
            print("Error! the queue is empty")
            return

        node = self.queue.pop()

        return node

    def enqueue(self, newData):
        if self.isReversed:
            # need to reverse to push to the last element
            self.reverse()

        self.queue.push(newData)

    def print(self):
        self.queue.print()