logo CodeStepByStep logo

collectionMystery9

Language/Type: C++ collections Stack Queue

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

void collectionMystery9(Queue<int>& queue, int p) {
    Stack<int> stack;
    int count = 0;
    int size = queue.size();
    for (int i = 0; i < size; i++) {
        int element = queue.dequeue();
        if (element < p) {
            queue.enqueue(element);
        } else {
            count++;
            stack.push(count);
            stack.push(element);
        }
    }
    
    while (!stack.isEmpty()) {
        queue.enqueue(stack.pop());
    }
    
    cout << queue << 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.