コード例 #1
0
        /// <summary>
        /// Remove the top elements from the stack
        /// </summary>
        /// <returns>Top element in stack</returns>
        public string Pop()
        {
            string returnValue = null;

            if (Count == 0)                                     // If there are no elements in the stack
            {
                returnValue = "Error! Cannot Pop from an empty stack";
            }
            else
            {
                LinkedStackNode current = this.Tail;

                returnValue = current.Data;

                if (current.Previous != null)                   // If we are somewhere in the stack
                {
                    this.Tail = current.Previous;
                }
                else if (current.Previous == Head)              // If this is the last element in the stack
                {
                    this.Tail = this.Head;
                }
                else                                            // If there are no more elements in the stack
                {
                    this.Head = null;
                    this.Tail = null;
                }

                Count--;
            }

            return(returnValue);
        }
コード例 #2
0
        /// <summary>
        /// Print all notes in the stack
        /// </summary>
        public void PrintStack()
        {
            if (Count == 0)                                     // If the stack is empty
            {
                Console.WriteLine("There is nothing to print, the Stack is empty");
            }
            else                                                // If the stack is not empty
            {
                LinkedStackNode current = Tail;

                for (int i = this.Count; i > 0; i--)
                {
                    Console.WriteLine(current.Data);

                    current = current.Previous;
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Constructor for node
 /// </summary>
 /// <param name="data"></param>
 public LinkedStackNode(string data)
 {
     this.Data     = data;
     this.Previous = null;
     this.Next     = null;
 }