Beispiel #1
0
        public void IsBalanceParentheses()
        {
            bool valid = false;

            Console.WriteLine("Enter Arithmetic Expression:");
            string expression = Console.ReadLine();

            char[] charArray = expression.ToCharArray();
            int    size      = charArray.Length;

            for (int j = 0; j < size; j++)
            {
                if (charArray[j] == '(' || charArray[j] == '{' || charArray[j] == '[' || charArray[j] == ')' || charArray[j] == '}' || charArray[j] == ']')
                {
                    valid = true;
                    break;
                }
            }
            if (valid)
            {
                StackArray stack = new StackArray(size);
                int        i     = 0;
                while (i < size)
                {
                    if (charArray[i] == '(' || charArray[i] == '{' || charArray[i] == '[')
                    {
                        stack.Push(charArray[i]);
                    }
                    else if (charArray[i] == ')' || charArray[i] == '}' || charArray[i] == ']')
                    {
                        stack.Pop();
                    }
                    i++;
                }
                if (stack.IsEmpty())
                {
                    Console.WriteLine("parentheses appeared in a balanced fashion");
                }
                else
                {
                    Console.WriteLine("parentheses are not appeared in a balanced fashion");
                }
            }
            else
            {
                Console.WriteLine("there is no parentheses!!!");
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // declare a Stack of strings call myStack
            var myStack = new StackArray <string>();

            myStack.Push("apples");
            myStack.Push("milk");
            myStack.Push("ham");
            myStack.Push("pears");

            Console.WriteLine("Number of items in stack: " + myStack.Count);
            Console.Write("Peeking the top: ");
            Console.WriteLine(myStack.Peek());
            Console.WriteLine("Popping all elements");

            // Pop and output all the elements of the
            while (!myStack.Empty())
            {
                Console.WriteLine(myStack.Pop());
            }


            Console.ReadLine();
        }