logo CodeStepByStep logo

collectionMystery10

Language/Type: C++ collections stack queue STL

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

void collectionMystery10(stack<int>& s, int n) {
    stack<int> s2;
    q<int> q;

    while (s.size() > n) {
        q.push(s.top());
        s.pop();
    }
    while (!s.empty()) {
        int element = s.top();
        s.pop();
        s2.push(element);
        if (element % 2 == 0) {
            q.push(element);
        }
    }
    while (!q.empty()) {
        s.push(q.front());
        q.pop();
    }
    while (!s2.empty()) {
        s.push(s2.top());
        s2.pop();
    }

    cout << s << 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

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.