Example #1
0
        public void QueueUsingTwoStacksBruteForceSolution()
        {
            // With this approach I was going with always keeping the dequeueStack empty and always re inserting the data back and forth between the stacks
            // This approach works but it is very slow, as it goes linearly along the enqueueStack stack to populate the dequeueStack stack.
            // after doing the Pop(), it will traverse back to re populate the enqueueStack back from the dequeueStack.

            var t            = Convert.ToInt32(Console.ReadLine());
            var enqueueStack = new QueueingStack();
            var dequeueStack = new QueueingStack();
            int intData;
            int poppedInt;

            for (int tItr = 0; tItr < t; tItr++)
            {
                var s = Console.ReadLine();

                switch (s[0])
                {
                case '1':
                    intData = int.Parse(s.Split(' ').Last());
                    enqueueStack.Push(intData);
                    break;

                case '2':
                    while (!enqueueStack.IsEmpty())
                    {
                        poppedInt = enqueueStack.Pop();
                        dequeueStack.Push(poppedInt);
                    }
                    dequeueStack.Pop();
                    while (!dequeueStack.IsEmpty())
                    {
                        poppedInt = dequeueStack.Pop();
                        enqueueStack.Push(poppedInt);
                    }
                    break;

                case '3':
                    while (!enqueueStack.IsEmpty())
                    {
                        poppedInt = enqueueStack.Pop();
                        dequeueStack.Push(poppedInt);
                    }
                    dequeueStack.Print();
                    while (!dequeueStack.IsEmpty())
                    {
                        poppedInt = dequeueStack.Pop();
                        enqueueStack.Push(poppedInt);
                    }
                    break;

                default:
                    throw new Exception("Invalid Input!");
                }
            }
        }
Example #2
0
        public void QueueUsingTwoStacksInprovedBruteForceSolution()
        {
            // This is a possible improvement to the brute force solution but it's still not optimized
            // Notice that the enqueueStack is repopulated from the dequeueStack
            // Important to notice these approaches are not optimized for performance and will take some time to perform.
            // These approaches were too focused on keeping one of the stacks always empty.
            // This was doing too much extra work.

            var t            = Convert.ToInt32(Console.ReadLine());
            var enqueueStack = new QueueingStack();
            var dequeueStack = new QueueingStack();
            int intData;
            int poppedInt;

            for (int tItr = 0; tItr < t; tItr++)
            {
                var s = Console.ReadLine();

                switch (s[0])
                {
                case '1':
                    intData = int.Parse(s.Split(' ').Last());
                    while (!dequeueStack.IsEmpty())
                    {
                        poppedInt = dequeueStack.Pop();
                        enqueueStack.Push(poppedInt);
                    }
                    enqueueStack.Push(intData);
                    break;

                case '2':
                    while (!enqueueStack.IsEmpty())
                    {
                        poppedInt = enqueueStack.Pop();
                        dequeueStack.Push(poppedInt);
                    }
                    dequeueStack.Pop();
                    break;

                case '3':
                    while (!enqueueStack.IsEmpty())
                    {
                        poppedInt = enqueueStack.Pop();
                        dequeueStack.Push(poppedInt);
                    }
                    dequeueStack.Print();
                    break;

                default:
                    throw new Exception("Invalid Input!");
                }
            }
        }
Example #3
0
        public void QueueUsingTwoStacksWorker()
        {
            // This is the optimized version
            // With this approach case 1 we only worry about pushing data to the enqueueStack
            // In case 2, if the dequeueStack is empty then we Pop() the enqueueStack, get the data that was popped, and Push() the data to the dequeueStack.
            // After we made sure the dequeueStack is not empty, then we Pop() the Top value
            // In case 3, if the dequeueStack is empty then we Pop() the enqueueStack, get the data that was popped, and Push() the data to the dequeueStack.
            // After we made sure the dequeueStack is not empty, then we Print() the Top value

            var t            = Convert.ToInt32(Console.ReadLine());
            var enqueueStack = new QueueingStack();
            var dequeueStack = new QueueingStack();
            int intData;
            int poppedInt;

            for (int tItr = 0; tItr < t; tItr++)
            {
                var s = Console.ReadLine();

                switch (s[0])
                {
                case '1':
                    intData = int.Parse(s.Split(' ').Last());

                    enqueueStack.Push(intData);

                    break;

                case '2':
                    if (dequeueStack.IsEmpty())
                    {
                        while (!enqueueStack.IsEmpty())
                        {
                            poppedInt = enqueueStack.Pop();

                            dequeueStack.Push(poppedInt);
                        }
                    }

                    dequeueStack.Pop();

                    break;

                case '3':
                    if (dequeueStack.IsEmpty())
                    {
                        while (!enqueueStack.IsEmpty())
                        {
                            poppedInt = enqueueStack.Pop();

                            dequeueStack.Push(poppedInt);
                        }
                    }

                    dequeueStack.Print();

                    break;

                default:
                    throw new Exception("Invalid Input!");
                }
            }
        }