public Form1()
        {
            InitializeComponent();
            Stack      stack   = new Stack();
            StringNode string2 = new StringNode("string2");
            StringNode string1 = new StringNode("string1", string2);

            stack.Push(string1.Data);
            Console.WriteLine(stack.Peek());
        }
        //Returns the number of strings currently held in the stack
        public int Count()
        {
            int        count   = 0;
            StringNode current = head;

            while (current != null)
            {
                count++;
                current = current.Next;
            }
            return(count);
        }
        //adds new string to the stack
        public void Push(string newString)
        {
            StringNode newNode = new StringNode(newString);

            //if stack is empty
            if (tail == null)
            {
                head = newNode;
                tail = newNode;
            }
            else
            {
                tail.Next = newNode;
                tail      = newNode;
            }
        }
        private void DeleteTail()
        {
            //If you are deleting the first node
            if (tail == head)
            {
                //(it is the only node in the list)
                //Head and Tail both become null
                head = null;
                tail = null;
            }
            else
            {
                StringNode current = head;
                while (current.Next != tail)
                {
                    current = current.Next;
                }
                current.Next = null;

                //Set Tail to point to the “previous” node, because it is now at the end
                tail = current;
            }
        }
Ejemplo n.º 5
0
 public StringNode(string data, StringNode next = null)
 {
     Data = data;
     Next = next;
 }
 public Stack()
 {
     head = null;
     tail = null;
 }