Ejemplo n.º 1
0
        /// <summary>
        /// Removes the node at the front of the queue, and returns it or throws an exception if the queue is empty.
        /// </summary>
        /// <returns>The removed node.</returns>
        public Node Dequeue()
        {
            if (IsEmpty())
            {
                throw new Exception("Cannot dequeue because the queue is empty.");
            }
            Node poppedNode = FrontStack.Pop();

            Front = FrontStack.Top;
            return(poppedNode);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Uses a second, temporary stack to emulate the action of Dequeueing a Node in a Queue
        /// via transferring data back and forth between the Stacks
        /// Removes the Node from the PseudoQueue
        /// </summary>
        /// <returns>Node that is removed from the Front of the PseudoQueue</returns>
        public Node <T> Dequeue()
        {
            Node <T>    returnTemp = null;
            MyStack <T> StackTwo   = new MyStack <T>();

            while (StackOne.Top != null)
            {
                Node <T> temp = StackOne.Pop();
                StackTwo.Push(temp);
            }

            if (StackTwo.Top != null)
            {
                returnTemp = StackTwo.Pop();
            }

            while (StackTwo.Top != null)
            {
                Node <T> temp = StackTwo.Pop();
                StackOne.Push(temp);
            }

            return(returnTemp);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Uses a second, temporary stack to emulate the action of Enqueueing a Node in a Queue
        /// via transferring data back and forth between the Stacks
        /// Adds the Node to the PseudoQueue
        /// </summary>
        /// <param name="node">Node to add to the Rear of the PseudoQueue</param>
        public void Enqueue(Node <T> node)
        {
            MyStack <T> StackTwo = new MyStack <T>();

            while (StackOne.Top != null)
            {
                Node <T> temp = StackOne.Pop();
                StackTwo.Push(temp);
            }

            StackOne.Push(node);

            while (StackTwo.Top != null)
            {
                Node <T> temp = StackTwo.Pop();
                StackOne.Push(temp);
            }
        }