logo CodeStepByStep logo

CollectionMystery3

Language/Type: C# collections Stack Queue

Write the output produced by the following method when passed each of the following queues:

public static void CollectionMystery3(Queue<int> q)
{
    Stack<int> s = new Stack<int>();
    int size = q.Count;
    for (int i = 0; i < size; i++)
    {
        int n = q.Dequeue();
        if (n % 2 == 0)
        {
            s.Push(n);
        }
        else
        {
            q.Enqueue(n);
        }
    }
    Console.WriteLine("q=" + q);
    Console.WriteLine("s=" + s);
}
{1, 2, 3, 4, 5, 6}
{42, -3, 4, 15, 9, 71}
{30, 20, 10, 60, 50, 40, 3, 0}

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.