logo CodeStepByStep logo

collectionMystery10

Language/Type: C++ collections Stack Queue
Author: Marty Stepp (on 2016/08/27)

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

void collectionMystery10(Stack<int>& stack, int n) {
    Stack<int> stack2;
    Queue<int> queue;

    while (stack.size() > n) {
        queue.enqueue(stack.pop());
    }
    while (!stack.isEmpty()) {
        int element = stack.pop();
        stack2.push(element);
        if (element % 2 == 0) {
            queue.enqueue(element);
        }
    }
    while (!queue.isEmpty()) {
        stack.push(queue.dequeue());
    }
    while (!stack2.isEmpty()) {
        stack.push(stack2.pop());
    }

    cout << stack << endl;
}
{1, 2, 3, 4, 5, 6}, n=3
{67, 29, 115, 84, 33, 71, 90}, n=5

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.

© Marty Stepp, all rights reserved.