/// <summary>
        /// Returns true if '(' and ')' balance, else false
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsBalanced(string input)
        {
            Stack_LinkedListBased <char> stack = new Stack_LinkedListBased <char>();

            char[] tokens = input.ToCharArray();

            foreach (char c in tokens)
            {
                if (c == '(')
                {
                    stack.Push(c);
                }
                else if (c == ')')
                {
                    if (stack.Count == 0)
                    {
                        return(false);
                    }
                    else
                    {
                        stack.Pop();
                    }
                }
            }
            if (stack.Count == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        public static void TestStackLinked()
        {
            var myStack = new Stack_LinkedListBased <int>();

            try
            {
                myStack.Pop();
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Hooray, it was empty and failed. :-)");
            }

            myStack.Push(1);
            myStack.Push(2);
            var popped = myStack.Pop();

            if (popped == 2)
            {
                Console.WriteLine("Yuhu... Found the value I pushed! :-D ");
            }

            myStack.Push(0);
            myStack.Push(3);
            myStack.Push(4);
            myStack.Push(5);
            Console.WriteLine($"Peeking at top value: {myStack.Peek()}");

            Console.WriteLine("Testing ToArray() ...");
            int[] returnedArray = myStack.ToArray();
            foreach (var item in returnedArray)
            {
                Console.Write($"{item} ");
            }

            Console.WriteLine("\nTesting IEnumerable ...");
            foreach (var item in myStack)
            {
                Console.Write($"{item} ");
            }

            Console.WriteLine("\nTesting Contains()...");
            var last = myStack.ToArray()[myStack.Count - 1];

            Console.WriteLine($"Last item in stack: {last}");
            Console.WriteLine($"Stack contains {last}: {myStack.Contains(last)}");

            Console.WriteLine("\nPopopopopop ...");
            for (int i = myStack.Count; i > 0; i--)
            {
                Console.WriteLine(i + " : " + myStack.Pop());
            }

            try
            {
                var myStackNull = new Stack_LinkedListBased <string>(null);
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Cannot pass null to constructor\n");
            }

            var myStack2 = new Stack_LinkedListBased <string>(new string[] { "alice", "bradley", "claire", "daniel", "edith" });

            foreach (var item in myStack2)
            {
                Console.WriteLine(item);
            }
        }