logo CodeStepByStep logo

collectionMystery7

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: Note that the queues below are written in {front ... back} order.

void collectionMystery7(Queue<int>& queue) {
    Stack<int> stack;
    int qsize = queue.size();
    for (int i = 0; i < qsize; i++) {
        if (queue.peek() % 2 == 0) {
            queue.enqueue(queue.dequeue());
        } else {
            stack.push(queue.peek());
            stack.push(queue.dequeue());
        }
    }
    while (!queue.isEmpty()) {
        stack.push(queue.dequeue());
    }
    while (!stack.isEmpty()) {
        cout << stack.pop() << " ";
    }
}
{1, 2, 3, 4, 5, 6}
{55, 33, 0, 88, 44, 99, 77, 66}
{80, 20, 65, 10, 5, 3, 40, 2, 11}

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.