// Removes and returns the Node on top of the stack, resetting the value of Top to the next Node on the stack.
        // Returns null if there is no Node on the stack.
        public CharNode Pop()
        {
            CharNode popped = null;

            try
            {
                if (Top != null)
                {
                    popped      = Top;
                    Top         = Top.Next;
                    popped.Next = null;
                }
                else
                {
                    throw new NullReferenceException();
                }
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("There are no Nodes in this Stack.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(popped);
        }
        /// <summary>
        /// adds a node to the stack
        /// </summary>
        /// <param name="value">value of the new node</param>
        public void Push(char value)
        {
            CharNode newNode = new CharNode(value);

            newNode.Next = Top;
            Top          = newNode;
        }
 // Takes in a new Node and places it on top of the stack, resetting the value of Top to the new Node.
 public void Push(CharNode node)
 {
     if (Top == null)
     {
         Top = node;
     }
     else
     {
         node.Next = Top;
         Top       = node;
     }
 }
 /// <summary>
 /// remove a node from the stack
 /// </summary>
 /// <returns>the top node</returns>
 public CharNode Pop()
 {
     //should add check for if trying to pop from a null stack
     if (Top == null)
     {
         Console.WriteLine("The stack is empty");
         return(null);
     }
     else
     {
         CharNode tempNode = Top;
         Top           = Top.Next;
         tempNode.Next = null;
         return(tempNode);
     }
 }
Пример #5
0
 public CharNode(char value)
 {
     Value = value;
     Next  = null;
 }
 //stack created with a node given
 public CharStack(CharNode node)
 {
     Top = node;
 }
 //instantiate
 //stack created without a node given
 public CharStack()
 {
     Top = null;
 }
Пример #8
0
 public CharNode(char val, CharNode next)
 {
     Value = val;
     Next  = next;
 }