logo CodeStepByStep logo

mystery1

Language/Type: Java stacks and queues

Write the output produced when the following method is passed each of the following stacks:

public static void mystery1(Stack<Integer> s) {
    Queue<Integer> q = new LinkedList<Integer>();
    while (!s.isEmpty()) {
        q.add(s.peek());
        q.add(s.pop());
    }
    while (!q.isEmpty()) {
        s.push(q.peek());
        q.remove();
    }
    System.out.println(s);
}
[2, 3, 1]
[42, -1, 4, 15, 9]
[30, 20, 10, 70, 50, 40]

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.