예제 #1
0
        /// <summary>
        /// Mimic a Queue object's enqueue method with stacks,
        /// and returns the value what would be the front object
        /// </summary>
        /// <param name="value"> value to enqueue </param>
        /// <returns></returns>
        public T Enqueue(T value)
        {
            Stack1.Push(value);
            while (!Stack1.IsEmpty())
            {
                Stack2.Push(Stack1.Pop());
            }

            T frontValue = Stack2.Peek();

            while (!Stack2.IsEmpty())
            {
                Stack1.Push(Stack2.Pop());
            }

            return(frontValue);
        }
예제 #2
0
        /// <summary>
        /// Mimic a Queue object's dequeue method with stacks.
        /// Pop() what would be the front node and return the value
        /// </summary>
        /// <returns></returns>
        public T Dequeue()
        {
            try
            {
                while (!Stack1.IsEmpty())
                {
                    Stack2.Push(Stack1.Pop());
                }

                T frontValue = Stack2.Pop();

                while (!Stack2.IsEmpty())
                {
                    Stack1.Push(Stack2.Pop());
                }

                return(frontValue);
            }
            catch (NullReferenceException e)
            {
                throw new NullReferenceException($"Your stack is empty, womp womp\n{e}");
            }
        }