Question: Python mirror queue and reverse queue 1. Write a function, mirror_queue(q) , which takes a Queue, q, as a parameter and modifies the queue so
Python mirror queue and reverse queue
1.
Write a function, mirror_queue(q), which takes a Queue, q, as a parameter and modifies the queue so that the queue items appear in their original order followed by a copy of the queue items in reverse order. You must make use of a stack to help you to solve this problem.
The implementations of both the Stack and Queue ADTs are provided to you. You can simply use: push(), pop(), is_empty() as well as enqueue() and dequeue() as necessary in your function definition.
However, you will need to use the following statement to create a Stack (in this question)
s = my_stack_module.Stack()
Similarly, you will need to use the following statement to create a Queue (in this question)
q = my_queue_module.Queue()
For example:
| Test | Result |
|---|---|
q1 = my_queue_module.Queue() q1.enqueue(1) q1.enqueue(2) q1.enqueue(3) mirror_queue(q1) print(q1) | Queue: [1, 2, 3, 3, 2, 1] |
q1 = my_queue_module.Queue() mirror_queue(q1) print(q1) | Queue: [] |
2.
Write a function, reverse_queue(q), which takes a Queue, q, as a parameter and modifies the queue so that the elements in the queue are rearranged into reverse order. You must make use of a stack to help you in reversing the elements in the queue.
The implementations of both the Stack and Queue ADTs are provided to you. You can simply use: push(), pop(), is_empty() as well as enqueue() and dequeue() as necessary in your function definition.
However, you will need to use the following statement to create a Stack (in this question)
s = my_stack_module.Stack()
Similarly, you will need to use the following statement to create a Queue (in this question)
q = my_queue_module.Queue()
For example:
| Test | Result |
|---|---|
q1 = my_queue_module.Queue() q1.enqueue(1) q1.enqueue(2) q1.enqueue(3) print(q1) reverse_queue(q1) print(q1) | Queue: [1, 2, 3] Queue: [3, 2, 1] |
q1 = my_queue_module.Queue() reverse_queue(q1) print(q1) | Queue: [] |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
