logo CodeStepByStep logo

collectionMystery8

Language/Type: C++ collections stack queue STL

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

void collectionMystery8(stack<int>& st) {
    queue<int> q;
    set<int> se;
    while (!st.empty()) {
        if (st.top() % 2 == 0) {
            q.push(st.top());
            st.pop();
        } else {
            se.insert(st.top());
            st.pop();
        }
    }
    for (int n : se) {
        st.push(n);
    }
    while (!q.empty()) {
        st.push(q.front());
        q.pop();
    }
    cout << st << endl;
}
{1, 2, 3, 4, 5}
{3, 2, 7, 3, 3, 4, 1, 1, 4}
{9, 7, 14, 7, 22, 7, 3, 14}
{8, 5, 1, 2, 1, 1, 2, 1, 4, 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.