logo Practice-It logo

mirror

Language/Type: Java Collections Stacks and Queues
Author: Stuart Reges (on 2014/02/13)

Write a method called mirror that takes a stack of integers as a parameter and that doubles the size of the stack by appending the mirror image of the original sequence at the top of the stack. The mirror image is the same sequence of values in reverse order. For example, suppose that a variable called s stores the following values:

        bottom [1, 3, 2, 7] top

and we make the following call:

        mirror(s);

then it should store the following values after the call:

        bottom [1, 3, 2, 7, 7, 2, 3, 1] top

Notice that it has been doubled in size by having the original sequence appearing in reverse order at the top of the stack. If the original stack is empty, your method should not change it.

You are to use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem and your solution must run in O(n) time where n is the size of the stack. Use the Stack and Queue interfaces and the ArrayStack and LinkedQueue implementations discussed in lecture.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.