logo CodeStepByStep logo

collectionMystery9

Language/Type: Java collections Stack Queue

Write the output produced by the following method when passed each of the following queues and ints. Note: A stack displays/prints in {bottom ... top} order, and a queue displays in {front ... back} order.

public static void collectionMystery9(Queue<Integer> queue, int p) {
    Stack<Integer> stack = new Stack<Integer>();
    int count = 0;
    int size = queue.size();
    for (int i = 0; i < size; i++) {
        int element = queue.remove();
        if (element < p) {
            queue.add(element);
        } else {
            count++;
            stack.push(count);
            stack.push(element);
        }
    }
    
    while (!stack.isEmpty()) {
        queue.add(stack.pop());
    }
    
    System.out.println(queue);
}
{1, 2, 3, 4, 5}, p=4
{67, 29, 115, 84, 33, 71, 90}, p=50

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.