public static bool CheckBrackets(string s)
        {
            string leftBracketString = "(";
            char   leftBracket       = leftBracketString[0];

            bool valid = true;
            int  count = 0;

            MyStack <string> stack = new MyStack <string>();

            foreach (char c in s)
            {
                if (c.Equals(leftBracket))
                {
                    stack.Push(c.ToString());
                    count++;
                }
                else
                {
                    if (!stack.IsEmpty())
                    {
                        if (stack.Top().Equals(leftBracketString))
                        {
                            stack.Pop();
                            count--;
                        }
                        else
                        {
                            valid = false;
                        }
                    }
                    else
                    {
                        valid = false;
                    }
                }
            }

            if (count != 0)
            {
                valid = false;
            }

            return(valid);
        }
Ejemplo n.º 2
0
        static void Opgave3()
        {
            System.Console.WriteLine("\n=====   Opgave 3 : MyStack   =====\n");

            MyStack <int> stack = new MyStack <int>();

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            System.Console.WriteLine(stack.Top());
            stack.Pop();
            stack.Pop();
            stack.Pop();
            try
            {
                stack.Pop();
            }
            catch (MyStackEmptyException e)
            {
                System.Console.WriteLine(e.Message);
            }
        }