コード例 #1
0
        /// <summary>
        /// Helper method
        /// </summary>
        private void AlternateNodes()
        {
            int   depth     = Count;
            bool  even      = Count % 2 == 0;
            Stack tempStack = new Stack();

            for (int i = 0; i < depth; i++)
            {
                if (even)
                {
                    tempStack.Push(StackTwo.Pop());
                }
                else
                {
                    tempStack.Push(StackOne.Pop());
                }
            }
            for (int j = 0; j < depth; j++)
            {
                if (even)
                {
                    StackOne.Push(tempStack.Pop());
                }
                else
                {
                    StackTwo.Push(tempStack.Pop());
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Method finds the beginning of the "Queue" or
 /// bottom of StackOne
 /// </summary>
 /// <returns></returns>
 public Node Peek()
 {
     while (StackOne.Peek() != null)
     {
         StackTwo.Push(StackOne.Pop());
     }
     Front = StackTwo.Top;
     while (StackTwo.Peek() != null)
     {
         StackOne.Push(StackTwo.Pop());
     }
     return(Front);
 }
コード例 #3
0
        /// <summary>
        /// Enqueue method
        /// </summary>
        /// <param name="value">The value of which we are trying to "enqueue"</param>
        public void Enqueue(string value)
        {
            AlternateNodes();

            if (Count % 2 != 0)
            {
                StackOne.Push(value);
            }
            else
            {
                StackTwo.Push(value);
            }
            Count++;
        }
コード例 #4
0
        /// <summary>
        /// Method uses the Stack Push and Pop methods to emulate FIFO.
        /// It will move all values from StackOne to StackTwo, pop and store the top
        /// from StackTwo and then proceed to re-fill StackOne to ensure Queue-Like
        /// Order
        /// </summary>
        /// <returns>Node that is removed</returns>
        public Node Dequeue()
        {
            while (StackOne.Peek() != null)
            {
                StackTwo.Push(StackOne.Pop());
            }
            Node popped = StackTwo.Pop();

            Front = StackTwo.Top;
            while (StackTwo.Peek() != null)
            {
                StackOne.Push(StackTwo.Pop());
            }
            return(popped);
        }
コード例 #5
0
 /// <summary>
 /// Method uses the Stack method Push to emulate adding to the
 /// back of a queue
 /// </summary>
 /// <param name="node">New node to be added</param>
 public void Enqueue(Node node)
 {
     StackOne.Push(node);
 }