logo CodeStepByStep logo

collectionMystery13

Language/Type: C++ collections stack queue STL
Author: Julie Zelenski (on 2021/07/06)

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

void collectionMystery13(queue<int>& q) {
    stack<int> s;
    int size = q.size();

    for (int i = 0; i < size; i++) {
        int val = q.front();
        q.pop();
        if (val < 0) {
            s.push(val);
        } else {
            q.push(val);
        }
    }
    while (!s.empty()) {
        q.push(s.top());
        s.pop();
    }
    cout << q << endl;
}
{-2, 7, -2, 7, 7}
{-8, 1, -8, -8, 3, 5}
{-6, -4, -3, -2, -7, 1, 1}
{-5, 3, 1, -4, 7, -9, 5, 2}

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.