コード例 #1
0
        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());
        }
コード例 #2
0
        private static int EvaluatePostfix(string postfix)
        {
            var result       = int.MinValue;
            int leftOperand  = int.MinValue;
            int rightOperand = int.MinValue;
            var stack        = new GenericStack <int>(postfix.Length);

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

                if (IsOperand(ch))
                {
                    stack.Push(int.Parse(ch.ToString()));
                }
                else
                {
                    rightOperand = stack.Pop();
                    leftOperand  = stack.Pop();
                    switch (ch)
                    {
                    case '+':
                    {
                        result = leftOperand + rightOperand;
                        stack.Push(result);
                        break;
                    }

                    case '-':
                    {
                        result = leftOperand - rightOperand;
                        stack.Push(result);
                        break;
                    }

                    case '*':
                    {
                        result = leftOperand * rightOperand;
                        stack.Push(result);
                        break;
                    }

                    case '/':
                    {
                        result = leftOperand / rightOperand;
                        stack.Push(result);
                        break;
                    }
                    }
                }
            }

            result = stack.Pop();
            return(result);
        }
コード例 #3
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);
        }
コード例 #4
0
        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);
        }