'How do I get a mirrored queue using stack?

I have defined the Stack and Queue class already so I can use any of the Queue methods - Queue(), enqueue(), dequeue(), peek(), size() and is_empty() and any of the Stack ADT methods: Stack(), push(), pop(), peek(), size() and is_empty().

What I basically need to do is modify the parameter Queue object so that the original queue items appear in their original order followed by a copy of the queue items in reverse order.

Here is what I have so far. But this is only giving me the reversed queue. Can someone help how I can modify the queue to both the original and reversed versions.

The Stack class:

class Stack:
    def __init__(self):
        self.items = []
    def is_empty(self):
        return self.items == []
    def push(self, item):
        self.items.append(item)
    def pop(self):
        if Stack.is_empty(self) == True:
            return None
        else:
            return self.items.pop()
    def peek(self):
        if Stack.is_empty(self) == True:
            return None
        else:
            return self.items[-1]
    def size(self):
        return len(self.items)

Queue class:

class Queue:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.insert(0,item)

    def dequeue(self):
        if len(self.items)==0:
            raise IndexError("ERROR: The queue is empty!")
        return self.items.pop()

    def size(self):
        return len(self.items)

    def peek(self):
        if Queue.is_empty(self) == True:
            raise IndexError("ERROR: The queue is empty!")
        else:
            return self.items[-1]
            
    def __eq__(self, other):
        self.other = other
        if self.items == self.other:
            return True
        else:
            return False
        
    def clear(self):
        del self.items[:]
        
    def __str__(self):
        return "{}".format(self.items[::-1])

Function I am required to define:

def mirror_queue(q):
    stack = Stack()

    while not q.is_empty():
        stack.push(q.dequeue())

    while not stack.is_empty():
        q.enqueue(stack.pop())

---test

q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
print(q1)
mirror_queue(q1)
print(q1)

---Expected Output

Queue: [1, 2, 3]
Queue: [1, 2, 3, 3, 2, 1] 

---Gotten Ouput

Queue: [1, 2, 3]
Queue: [3, 2, 1] 


Solution 1:[1]

mirror_queue seems to just reverse your queue.

My approach: Don’t change the original q but reverse a copy of q1 called q1r. Afterwards concat those two which then is your result (q1 + q1r).

Solution 2:[2]

When you use the function dequeue, it takes out one item from your queue, so when the first loop ends you have an empty queue and the stack with 3 elements. When you pop from your stack it enqueue back to your queue in reverse order. You should make a copy of the elements of the queue without dequeue it in order to archive what you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 b3z
Solution 2 Clovis Ignacio Ferreira