Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Java. Thank you 3) Quoues and stacks: Write a method called mirror( t

ID: 3904283 • Letter: P

Question

Please use Java. Thank you

3) Quoues and stacks: Write a method called mirror( that takes a stack of integers as a parameter and doubles the size of the stack by appending the mirror image of the original sequence at values in reverse order. For example, given a stack s with the following values : bottom [3, 1, 4, 1, 5, 9] top after calling mirror(s), it should store: bottom 3, 1, 4, 1, 5, 9, 9, 5, 1,4, 1, 3] top If the original stack is empty your method should not change it. If the stack passed to mirror is null, you should throw an IllegalArgumentException. You should use one queue as auxiliary storage. You are not allowed to use other auxiliary data structures. You may not solve the problem recursively. You may not use peek) or any iterators

Explanation / Answer

import java.util.ArrayDeque; import java.util.Queue; import java.util.Stack; public class Mirror { public static void mirror(Stack stack) { Stack tempStack = new Stack(); Queue tempQueue = new ArrayDeque(); int temp; while (!stack.isEmpty()) { temp = stack.pop(); tempStack.push(temp); tempQueue.add(temp); } while (!tempStack.isEmpty()) { stack.push(tempStack.pop()); } while (!tempQueue.isEmpty()) { stack.push(tempQueue.remove()); } } public static void main(String[] args) { Stack stack = new Stack(); stack.push(3); stack.push(1); stack.push(4); stack.push(1); stack.push(5); stack.push(9); System.out.println(stack); mirror(stack); System.out.println(stack); } }