3
\$\begingroup\$

Write a method mirror that accepts a stack of integers as a parameter and replaces the stack contents with itself plus a mirrored version of itself (the same elements in the opposite order). For example, suppose a variable s stores the following elements:

bottom [10, 50, 19, 54, 30, 67] top

After a call of mirror(s);, the stack would store the following elements (underlined for emphasis):

bottom [10, 50, 19, 54, 30, 67, 67, 30, 54, 19, 50, 10] top

Note that the mirrored version is added on to the top of what was originally in the stack. The bottom half of the stack contains the original numbers in the same order. If your method is passed an empty stack, the result should be an empty stack. If your method is passed a null stack, your method should throw an IllegalArgumentException.

You may use one stack or one queue (but not both) 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. For full credit your code must run in O(n) time where n is the number of elements of the original stack. Use the Queue interface and Stack/LinkedList classes from lecture.

I'm looking for ways to improve my future programming. Is this a good way to solve this problem? Currently, I'm surprised with the amount of work I'm doing with these Stacks and Queues data structures.

Yes, I know I'm being asked to use these data structures to solve the problem. But maybe there are ways that I need to think outside of the box to accomplish the same task?

I'm also looking for any bugs my code might have, so definitely let me know if you find any!

public static void mirror (Stack<Integer> s) {

    if (s == null) {
        throw new IllegalArgumentException();
    }

    Queue<Integer> q = new LinkedList<Integer>();

    while (!s.isEmpty()) {
        q.offer(s.pop());
    }

    while (!q.isEmpty()) {
        s.push(q.poll());
    }

    while (!s.isEmpty()) {
        q.offer(s.pop());
    }

    int size = q.size();
    for (int i = 0; i < size; i++) {
        int valq = q.poll();
        s.push(valq);
        q.offer(valq);
    }

    while (!s.isEmpty()) {
        q.offer(s.pop());
    }

    while (!q.isEmpty()) {
        s.push(q.poll());
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What parts of Stack/Queue are you allowed to use? You could iterate over the stack with a foreach loop, but that's not the pure interface of a stack. Similarly, Queue#size() isn't part of the pure interface of a queue. \$\endgroup\$ – Justin Dec 11 '17 at 3:04
  • \$\begingroup\$ If you are allowed to iterate over a Stack using the fact that it is Iterable, there's a pretty simple way to do this \$\endgroup\$ – Justin Dec 11 '17 at 3:08
  • 1
    \$\begingroup\$ Or if you are allowed to use the fact that Stack implements the List interface in Java, you can make this exceedingly simple (I have a 4 line implementation or that) \$\endgroup\$ – Justin Dec 11 '17 at 3:15
  • 1
    \$\begingroup\$ Feel free to post whichever solution(s) you like. I have the problem statement at the top, but I'm okay with anything. I just want to learn more about programming in general, so any way to solve a problem is 👌 \$\endgroup\$ – cody.codes Dec 11 '17 at 19:55
2
\$\begingroup\$

Overall this code looks decent. Just a few concerns:

  • For this kind of complex set of steps, it is always a good idea to add comments which will help the reader to understand the code

  • You can use the diamond operator here:

    Queue<Integer> q = new LinkedList<>();
    
  • I might be opinionated, but I prefer the name val (or even v) rather than valq.

    for (int i = 0; i < size; i++) {
        int val = q.poll();
        s.push(val);
        q.offer(val);
    }
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.