示例#1
0
        //return the last added string node and remove it frok the stack
        public String Pop()
        {
            string returnString;
            //if the header == null the stack is empty, throw an exception.
            if(headpointer == null)
            {
                throw new NullReferenceException("Cannot pop an EmptyStack, no string to return");
            }
            //else if the head and tail are the same thing there is only 1 node in the stack, return this and clear the stack.
            else if(headpointer == tailPointer)
            {
                returnString = tailPointer.stringObject;

                headpointer = null;
                tailPointer = null;
            //else there is multiple nodes in the stack, set the return string as the tail node as we aways remove this on in a stack(LIFO).
            }else
            {
                returnString = tailPointer.stringObject;

                 //first check if there is 2 nodes
                if (headpointer.Next == tailPointer)
                {
                    //set the tail pointer to now point at the head, nd the tails .next to null as it no longer point at the node we just removed
                    tailPointer = headpointer;
                    tailPointer.Next = null;
                }
                else
                {
                    //else if there are more than 2 nodes, walk the stack untill you get to the node that's .next is the tail pointer, set the node walker as the tail and set its .next to null
                    StringNode nodeWalker = headpointer;

                    while (nodeWalker.Next != null)
                    {
                        nodeWalker = nodeWalker.Next;

                        if (nodeWalker.Next == tailPointer)
                        {
                            tailPointer = nodeWalker;
                            nodeWalker.Next = null;
                        }

                    }
                }
            }
            //return the string
            return returnString;
        }
示例#2
0
        //Method to push StringNode into stack
        public void push(String newString)
        {
            //create string node and set its sting property to the sting passed in
            StringNode newStringNode = new StringNode(newString);

            //if the string is null throw and exception
            if (newStringNode.stringObject == null)
            {
                throw new NullReferenceException("Cannot push null string to stack");
            }
            else
            {

                //Check stack is empty ,if so point the head and tail at new string node
                if (headpointer == null)
                {
                    headpointer = newStringNode;
                    tailPointer = newStringNode;

                    //Set the current tail pointers .net to point at the new node and set the new node to be the tail pointer.
                }
                else
                {
                    tailPointer.Next = newStringNode;
                    tailPointer = newStringNode;
                }
            }
        }