logo CodeStepByStep logo

CollectionMystery7

Language/Type: C# collections Stack Queue

Write the output produced by the following method when passed each of the following queues: Note that the queues below are written in {front ... back} order.

public static void CollectionMystery7(Queue<int> queue)
{
    Stack<int> stack = new Stack<int>();
    int qsize = queue.Count;
    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.Count != 0)
    {
        stack.Push(queue.Dequeue());
    }
    while (stack.Count != 0)
    {
        Console.Write(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

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.