logo CodeStepByStep logo

collectionMystery9

Language/Type: C++ collections Stack Queue
Author: Marty Stepp (on 2016/06/16)

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

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.