logo CodeStepByStep logo

collection_mystery3

Language/Type: PHP collections Stack Queue

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.

function collection_mystery3($queue) {
    $stack = [];
    $size = count($queue);
    for ($i = 0; $i < $size; $i++) {
        $n = array_shift($queue);
        if ($n % 2 == 0) {
            array_push($queue, $n);
        } else {
            array_push($stack, $n);
            array_push($stack, $n);
        }
    }
    while (count($queue) > 0) {
        array_push($stack, array_shift($queue));
    }
    while (count($stack) > 0) {
        print(array_pop($stack) . " ");
    }
}
[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

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.