logo CodeStepByStep logo

collectionMystery13

Language/Type: C++ collections Stack Queue
Author: Julie Zelenski (on 2019/05/11)

Write the output produced by the following function when passed each of the following queues. Recall: 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.dequeue();
        if (val < 0) {
            s.push(val);
        } else {
            q.enqueue(val);
        }
    }
    while (!s.isEmpty()) {
        q.enqueue(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.