logo CodeStepByStep logo

CollectionMystery10

Language/Type: C# collections Stack Queue

Write the output produced by the following method when passed each of the following stacks and ints. Note: A stack displays/prints in {bottom ... top} order, and a queue displays in {front ... back} order.

public static void CollectionMystery10(Stack<int> stack, int n)
{
    Stack<int> stack2 = new Stack<int>();
    Queue<int> queue = new Queue<int>();

    while (stack.Count > n)
    {
        queue.Enqueue(stack.Pop());
    }
    while (stack.Count != 0)
    {
        int element = stack.Pop();
        stack2.Push(element);
        if (element % 2 == 0)
        {
            queue.Enqueue(element);
        }
    }
    while (queue.Count != 0)
    {
        stack.Push(queue.Dequeue());
    }
    while (stack2.Count != 0)
    {
        stack.Push(stack2.Pop());
    }

    Console.WriteLine(stack);
}
{1, 2, 3, 4, 5, 6}, n=3
{67, 29, 115, 84, 33, 71, 90}, n=5

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.