示例#1
0
        public static void Main(string[] args)
        {
            Stack <String> wordStack = new Stack <String>();

            wordStack.push("hello");
            Console.WriteLine(wordStack.peek());
            wordStack.pop();
        }
示例#2
0
        public static bool MultiBracketValidation(string input)
        {
            Stack <char> bracketValidater = new Stack <char>();

            if (input.Length == 0)
            {
                throw new Exception("Empty String.");
            }

            foreach (char character in input)
            {
                if (character == '(' || character == '[' || character == '{')
                {
                    bracketValidater.Push(character);
                }

                switch (character)
                {
                case ')':
                    if (bracketValidater.IsEmpty())
                    {
                        return(false);
                    }

                    if (bracketValidater.peek() == '(')
                    {
                        bracketValidater.Pop();
                    }
                    break;

                case ']':
                    if (bracketValidater.IsEmpty())
                    {
                        return(false);
                    }

                    if (bracketValidater.peek() == '[')
                    {
                        bracketValidater.Pop();
                    }
                    break;

                case '}':
                    if (bracketValidater.IsEmpty())
                    {
                        return(false);
                    }

                    if (bracketValidater.peek() == '{')
                    {
                        bracketValidater.Pop();
                    }
                    break;

                default:
                    break;
                }
            }

            if (!bracketValidater.IsEmpty())
            {
                return(false);
            }

            return(true);
        }