logo CodeStepByStep logo

CollectionMystery6

Language/Type: C# collections Stack Queue

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

public static void CollectionMystery6(Stack<int> s)
{
    Queue<int> q = new Queue<int>();
    Stack<int> s2 = new Stack<int>();

    while (s.Count != 0)
    {
        if (s.Peek() % 2 == 0)
        {
            q.Enqueue(s.Pop());
        }
        else
        {
            s2.Push(s.Pop());
        }
    }

    while (q.Count != 0)
    {
        s.Push(q.Dequeue());
    }
    while (s2.Count != 0)
    {
        s.Push(s2.Pop());
    }

    Console.WriteLine(s);
}
{1, 2, 3, 4, 5, 6}
{42, 3, 12, 15, 9, 71, 88}
{65, 30, 10, 20, 45, 55, 6, 1}

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.