예제 #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 public Stack()
 {
     //Set headPointer and tailPointer to null(No items in Stack)
     headPointer = tailPointer = null;
 }
예제 #2
0
        /// <summary>
        /// Return last item on the Stack and remove it
        /// </summary>
        /// <returns>String of last item added to Stack</returns>
        public String Pop()
        {
            String result;

            //Grab data from last node
            result = Peek();

            //Remove last Node

            //Check if the stack has only one item
            if (headPointer == tailPointer)
            {
                headPointer = tailPointer = null;
            }
            else
            {
                //Prepare to walk list to find previous Node
                Node nodeWalker = headPointer;

                while (nodeWalker.Next != tailPointer)
                {
                    nodeWalker = nodeWalker.Next;
                }
                //Found previous Node
                //Set Next to null as it is now the last Node
                nodeWalker.Next = null;

                //Set tailPointer to last item in list
                tailPointer = nodeWalker;

            }
            return result;
        }
예제 #3
0
 /// <summary>
 /// Push a new item onto the Stack
 /// </summary>
 /// <param name="newString">Item to push onto the Stack</param>
 /// TODO Is a null string allowed or throw an exception?
 public void Push(String newString)
 {
     ///Create the new Node
     Node newNode = new Node(newString);
     //Add the new Node to the list
     //Check if the list is empty
     if (headPointer == null)
     {
         //Set headPointer and tailPointer to the new Node
         headPointer = tailPointer = newNode;
     }
     else
     {
         //Get the last Node and point it's next to the new Node
         tailPointer.Next = newNode;
         //Point tailPointer to the new Node
         tailPointer = newNode;
     }
 }