logo CodeStepByStep logo

collectionMystery7

Language/Type: C++ collections stack queue STL

Write the output produced by the following function when passed each of the following queues. Assume that a stack prints in {bottom, ..., top} order, and a queue displays in {front, ..., back} order.

void collectionMystery7(queue<int>& q) {
    stack<int> s;
    int qsize = q.size();
    for (int i = 0; i < qsize; i++) {
        if (q.front() % 2 == 0) {
            q.push(q.front());
            q.pop();
        } else {
            s.push(q.front());
            s.push(q.front());
            q.pop();
        }
    }
    while (!q.empty()) {
        s.push(q.front());
        q.pop();
    }
    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }
}
{1, 2, 3, 4, 5, 6}
{55, 33, 0, 88, 44, 99, 77, 66}
{80, 20, 65, 10, 5, 3, 40, 2, 11}

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.