static void Main(string[] args)
        {
            Console.WriteLine("Initializing Interger Stack");
            Console.WriteLine("Accepting size 3 : ");
            int size     = 3;
            var intStack = new GenericStack <int>(size);

            intStack.Push(1);
            intStack.Push(2);
            intStack.Push(3);

            Console.WriteLine("Is stack full : " + intStack.IsFull());
            Console.WriteLine("Peek element :{0}", intStack.Peek());
            Console.WriteLine("Pop element :{0}", intStack.Pop());
            Console.WriteLine("Pop element :{0}", intStack.Pop());
            Console.WriteLine("Pop element :{0}", intStack.Pop());
            Console.WriteLine("Is stack empty : " + intStack.IsEmpty());

            Console.WriteLine("Initializing Character Stack");
            Console.WriteLine("Accepting size 3 : ");
            var charStack = new GenericStack <char>(size);

            charStack.Push('A');
            charStack.Push('B');
            charStack.Push('C');

            Console.WriteLine("Is stack full : " + charStack.IsFull());
            Console.WriteLine("Peek element :{0}", charStack.Peek());
            Console.WriteLine("Pop element :{0}", charStack.Pop());
            Console.WriteLine("Pop element :{0}", charStack.Pop());
            Console.WriteLine("Pop element :{0}", charStack.Pop());
            Console.WriteLine("Is stack empty : " + charStack.IsEmpty());
        }
Ejemplo n.º 2
0
        private static string ToPostfix(string expression)
        {
            var postfix = string.Empty;
            var stack   = new GenericStack <char>(expression.Length);

            for (int i = 0; i < expression.Length; i++)
            {
                char ch = expression[i];
                if (IsOperand(ch))
                {
                    postfix = postfix + ch;
                }
                else
                {
                    if (stack.IsEmpty())
                    {
                        stack.Push(ch);
                    }
                    else
                    {
                        var precedence = Precedence(ch);
                        while ((Precedence(stack.Peek()) >= precedence) && !stack.IsEmpty())
                        {
                            var item = stack.Pop();
                            postfix = postfix + item;
                        }
                        stack.Push(ch);
                    }
                }
            }

            while (!stack.IsEmpty())
            {
                postfix = postfix + stack.Pop();
            }

            return(postfix);
        }
        private static bool CheckBalance(string str)
        {
            var stack = new GenericStack <char>(str.Length);

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '(' || str[i] == '[' || str[i] == '{')
                {
                    stack.Push(str[i]);
                }
                else if (str[i] == ')' || str[i] == ']' || str[i] == '}')
                {
                    if (stack.IsEmpty())
                    {
                        return(false);
                    }
                    char bracket = stack.Pop();
                    if (str[i] == ')' && bracket != '(')
                    {
                        return(false);
                    }
                    if (str[i] == ']' && bracket != '[')
                    {
                        return(false);
                    }
                    if (str[i] == '}' && bracket != '{')
                    {
                        return(false);
                    }
                }
            }

            if (stack.IsEmpty())
            {
                return(true);
            }
            return(false);
        }