logo CodeStepByStep logo

collectionMystery6

Language/Type: C++ collections Stack Queue

Write the output produced by the following function when passed each of the following stacks:

void collectionMystery6(Stack<int>& s) {
    Queue<int> q;
    Stack<int> s2;

    while (!s.isEmpty()) {
        if (s.peek() % 2 == 0) {
            q.enqueue(s.pop());
        } else {
            s2.push(s.pop());
        }
    }

    while (!q.isEmpty()) {
        s.push(q.dequeue());
    }
    while (!s2.isEmpty()) {
        s.push(s2.pop());
    }

    cout << s << endl;
}
{1, 2, 3, 4, 5, 6}
{42, 3, 12, 15, 9, 71, 88}
{65, 30, 10, 20, 45, 55, 6, 1}

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.