Exemplo n.º 1
0
        public static void PeekAtStackNode()
        {
            //Arrange
            StackAndQueue.Stack stack = new StackAndQueue.Stack(new Node(1));
            stack.Push(new Node(5));
            stack.Push(new Node(6));
            stack.Push(new Node(7));
            stack.Push(new Node(8));
            stack.Push(new Node(9));

            //Act
            stack.Peek();

            //Assert
            Assert.Equal(9, stack.Top.Value);
        }
Exemplo n.º 2
0
        public static void Parentheses()
        {
            Console.WriteLine("Please enter string with brackets:");
            string k = Console.ReadLine();
            Stack  stack = new Stack(k.Length);
            int    a = 0, b = 0;

            //Traverse string.
            for (int i = 0; i < k.Length; i++)
            {
                string str = k[i].ToString();

                //push left parenthesis to stack.
                if (str.Equals("(") || str.Equals("{") || str.Equals("["))
                {
                    stack.Push(k[i]);
                    a++;
                }

                //pop left parenthesis which is correspond right parenthesis
                while (str.Equals(")"))
                {
                    b++;
                    if (!stack.IsEmpty() && stack.Peek().ToString().Equals("("))
                    {
                        stack.Pop();
                        break;
                    }
                    else
                    {
                        Console.WriteLine("invalid Parentheses");
                        return;
                    }
                }

                while (str.Equals("]"))
                {
                    b++;
                    if (!stack.IsEmpty() && stack.Peek().ToString().Equals("["))
                    {
                        stack.Pop();
                        break;
                    }
                    else
                    {
                        Console.WriteLine("invalid Parentheses");
                        return;
                    }
                }

                while (str.Equals("}"))
                {
                    b++;
                    if (!stack.IsEmpty() && stack.Peek().ToString().Equals("{"))
                    {
                        stack.Pop();
                        break;
                    }
                    else
                    {
                        Console.WriteLine("invalid Parentheses");
                        return;
                    }
                }
            }
            if (stack.IsEmpty() && a - b == 0)
            {
                Console.WriteLine("Valid Parentheses");
            }
            else if (a > b)
            {
                Console.WriteLine("invalid Parentheses");
            }
        }