public void MorePushing()
        {
            var myStack = new BjsStack();

            myStack.Empty().ShouldBe(true);

            myStack.Push('h');
            myStack.Push('P');
            myStack.Push('q');
            myStack.Push('L');

            myStack.Empty().ShouldBe(false);

            myStack.TopNode().Payload.ShouldBe('L');
            myStack.TopNode().Next.Payload.ShouldBe('q');

            myStack.PopNode().Payload.ShouldBe('L');
            myStack.TopNode().Payload.ShouldBe('q');
            myStack.TopNode().Next.Payload.ShouldBe('P');

            myStack.PopNode().Payload.ShouldBe('q');
            myStack.TopNode().Payload.ShouldBe('P');
            myStack.TopNode().Next.Payload.ShouldBe('h');

            myStack.TopNode().Payload.ShouldBe('P');
            myStack.PopNode().Payload.ShouldBe('P');
            myStack.PopNode().Payload.ShouldBe('h');

            myStack.Empty().ShouldBe(true);
        }
Exemple #2
0
        public string BracketsBalanced(string input)
        {
            if (input.Length == 1)
            {
                return("1");
            }

            var bracketStack = new BjsStack();

            char[] openBracketChars  = { '{', '[', '(' };
            char[] closeBracketChars = { '}', ']', ')' };

            char top = 'a';

            for (int i = 0; i < input.Length; i++)
            {
                var c = input[i];

                if (i == input.Length - 1 && openBracketChars.Contains(c))
                {
                    return((i + 1).ToString());
                }

                if (openBracketChars.Contains(c))
                {
                    bracketStack.Push(c);
                }
                else if (closeBracketChars.Contains(c))
                {
                    if (bracketStack.Empty())
                    {
                        return((i + 1).ToString());
                    }

                    top = bracketStack.PopNode().Payload;

                    if ((top == '[' && c != ']') ||
                        (top == '{' && c != '}') ||
                        (top == '(' && c != ')'))
                    {
                        return((i + 1).ToString());
                    }
                }
            }

            if (!bracketStack.Empty())
            {
                top = bracketStack.Top.Payload;
                var unmatchedBracketIndex = input.IndexOf(top) + 1;
                return(unmatchedBracketIndex.ToString());
            }

            return("Success");
        }