static void Main(string[] args)
        {
            //Stack Operations
            Stack stack = new Stack();

            stack.Push(70);
            stack.Push(30);
            stack.Push(56);
            stack.Display();
            stack.Peek();
            stack.Pop();
            stack.IsEmpty();
            stack.Display();

            //Queue Operations
            Queue linkedListQueue = new Queue();

            linkedListQueue.Enqueue(56);
            linkedListQueue.Enqueue(30);
            linkedListQueue.Enqueue(70);
            linkedListQueue.Dequeue();
            linkedListQueue.Display();
        }
Example #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");
            }
        }