logo CodeStepByStep logo

collectionMystery3

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

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

void collectionMystery3(Queue<int>& q) {
    Stack<int> s;
    int size = q.size();
    for (int i = 0; i < size; i++) {
        int n = q.dequeue();
        if (n % 2 == 0) {
            s.push(n);
        } else {
            q.enqueue(n);
        }
    }
    cout << "q=" << q << endl;
    cout << "s=" << s << endl;
}
{1, 2, 3, 4, 5, 6}
{42, -3, 4, 15, 9, 71}
{30, 20, 10, 60, 50, 40, 3, 0}

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.