Esempio n. 1
0
        /**
         *  Removes the last node from the stack and returns it's data
         */
        public String Pop()
        {
            // Check the stack isn't empty
            if (IsEmpty())
                throw new InvalidOperationException();

            // Store the popped data
            String poppedData = tail.Data;

            // Remove the popped node from the stack
            if (head == tail)
            {
                // The popped node was the only in the stack
                head = null;
                tail = null;
            }
            else
            {
                // The popped data was not the only in the list
                StringNode nodeWalker = head;

                while (nodeWalker != null)
                {
                    if (nodeWalker.Next == tail)
                    {
                        nodeWalker.Next = null;
                        tail = nodeWalker;
                    }

                    nodeWalker = nodeWalker.Next;
                }
            }

            return poppedData;
        }
Esempio n. 2
0
        public int Count()
        {
            int count = 0;

            StringNode nodeWalker = top;

            while (nodeWalker != null)
            {
                count++;
                nodeWalker = nodeWalker.Next;
            }

            return(count);
        }
Esempio n. 3
0
        public void Push(string newString)
        {
            StringNode newNode = new StringNode(newString);

            if (top == null)
            {
                top = newNode;
            }
            else
            {
                newNode.Next = top;
                top          = newNode;
            }
        }
Esempio n. 4
0
        public String Pop()
        {
            try
            {
                //get the string from the last added node on stack
                String returnString = top.nodeString;

                //remove the top node from the stack
                top = top.Next;

                return(returnString);
            }
            //if nothing on the stack
            catch (NullReferenceException)
            {
                throw new NullReferenceException("no items in stack");
            }
        }
Esempio n. 5
0
 // Constructor
 public Stack()
 {
     head = null;
     tail = null;
 }
Esempio n. 6
0
 /**
  *  Adds a new node the the stack
  */
 public void Push(String newNodeData)
 {
     if (IsEmpty())
     {
         // If stack is empty
         StringNode newNode = new StringNode(newNodeData);
         head = newNode;
         tail = newNode;
     }
     else
     {
         // If stack has already got at least one node
         tail.Next = new StringNode(newNodeData);
         tail = tail.Next;
     }
 }
Esempio n. 7
0
 // Constructor
 public StringNode(String data)
 {
     this.data = data;
     next = null;
 }