logo CodeStepByStep logo

collectionMystery9

Language/Type: C++ collections stack queue STL

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

void collectionMystery9(queue<int>& q, int p) {
    stack<int> s;
    int count = 0;
    int size = q.size();
    for (int i = 0; i < size; i++) {
        int element = q.front();
        q.pop();
        if (element < p) {
            q.push(element);
        } else {
            count++;
            s.push(count);
            s.push(element);
        }
    }
    
    while (!s.empty()) {
        q.push(s.top());
        s.pop();
    }
    
    cout << q << endl;
}
{1, 2, 3, 4, 5}, p=4
{67, 29, 115, 84, 33, 71, 90}, p=50

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.