示例#1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Stack collections *****");

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(10);
            arrayStack.Push(20);
            arrayStack.Push(30);
            arrayStack.Push(40);
            arrayStack.Push(50);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());
        }
示例#2
0
文件: Program.cs 项目: dima92/Stack
        static void Main(string[] args)
        {
            var easyStack = new EasyStack <int>();

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

            var item  = easyStack.Pop();
            var item2 = easyStack.Peek();

            Console.WriteLine(item);
            Console.WriteLine(item2);
            Console.ReadLine();

            var linkedStack = new LinkedStack <int>();

            linkedStack.Push(10);
            linkedStack.Push(20);
            linkedStack.Push(30);


            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Peek());
            Console.ReadLine();

            var arrayStack = new ArrayStack <int>(5);

            Console.WriteLine(arrayStack.Count);
            arrayStack.Push(100);
            Console.WriteLine(arrayStack.Count);
            arrayStack.Push(200);
            arrayStack.Push(300);
            arrayStack.Push(400);
            arrayStack.Push(500);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());

            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            SimpleStack <int> myStack = new SimpleStack <int>();

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

            var item       = myStack.Pop();
            var secondItem = myStack.Peek();

            Console.WriteLine(item);
            Console.WriteLine(secondItem);
            Console.ReadLine();

            Stack <double> stack = new Stack <double>();

            var linkedStack = new LinkedStack <int>();

            linkedStack.Push(10);
            linkedStack.Push(20);
            linkedStack.Push(30);
            linkedStack.Push(40);

            Console.WriteLine(linkedStack.Peek());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Pop());
            Console.WriteLine(linkedStack.Peek());
            Console.ReadLine();

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(100);
            arrayStack.Push(200);
            arrayStack.Push(300);
            arrayStack.Push(400);
            arrayStack.Push(500);

            System.Console.WriteLine(arrayStack.Peek());
            System.Console.WriteLine(arrayStack.Pop());
            System.Console.WriteLine(arrayStack.Pop());
            System.Console.WriteLine(arrayStack.Peek());

            Console.ReadLine();
        }
示例#4
0
        static void Main(string[] args)
        {
            ArrayStack <int> s = new ArrayStack <int>(10);

            s.Push(1);
            s.Push(2);
            s.Push(3);
            s.Push(4);
            s.Push(5);
            s.Pop();
            s.Push(6);
            s.Push(7);
            //s.ToString();

            ExpressionCalculation expression = new ExpressionCalculation("5-1-2");

            expression.Result();
            Console.ReadLine();
        }
示例#5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var stack = new ArrayStack <int>();

            stack.Print();
            stack.Push(1);
            stack.Print();
            stack.Push(2);
            stack.Print();
            stack.Push(3);
            stack.Print();
            var res = stack.Pop();

            Console.WriteLine("Pop:" + res);
            stack.Print();
            DecToBinary(25);
            Console.ReadLine();
        }
示例#6
0
        /// <summary>
        /// 十进制转二进制
        /// </summary>
        static void DecToBinary(int num)
        {
            var n       = num;
            var stack   = new ArrayStack <int>();
            var residue = 0;

            while (num != 0)
            {
                residue = num % 2;
                stack.Push(residue);
                num = num / 2;
            }
            stack.Print();
            var result = "Binary:";

            while (stack.Size > 0)
            {
                result += stack.Pop();
            }
            Console.WriteLine("Dec:" + n + "->" + result);
        }
示例#7
0
        static void Main(string[] args)
        {
            var easyStack = new EasyStack <int>();

            easyStack.Push(1);
            easyStack.Push(4);
            easyStack.Push(2);
            easyStack.Push(6);

            var item = easyStack.Pop();

            var item2 = easyStack.Peek();

            Console.WriteLine(item);
            Console.WriteLine(item2);

            Console.WriteLine();

            Stack <double> stack = new Stack <double>();

            var linkedStack = new LinkedStack <int>();

            var arrayStack = new ArrayStack <int>(5);

            arrayStack.Push(101);
            arrayStack.Push(10);
            arrayStack.Push(33);
            arrayStack.Push(20);
            arrayStack.Push(15);

            Console.WriteLine(arrayStack.Peek());
            Console.WriteLine(arrayStack.Pop());
            Console.WriteLine(arrayStack.Peek());

            Console.WriteLine(arrayStack.ToString());


            Console.ReadLine();
        }
        private static void Main()
        {
            ArrayStack <int> array = new ArrayStack <int>(5);

            Console.WriteLine($"Кол-во элементов в стеке = {array.Count}");

            array.Push(100);
            array.Push(200);
            array.Push(300);
            array.Push(400);
            array.Push(500);

            Console.WriteLine($"Кол-во элементов в стеке = {array.Count}");

            Console.WriteLine(array.Peek());

            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());
            Console.WriteLine(array.Pop());

            Console.WriteLine($"Кол-во элементов в стеке = {array.Count}");
        }
        internal void Run()
        {
            ArrayStack numStack  = new ArrayStack(10);
            ArrayStack operStack = new ArrayStack(10);

            int    index = 0;//指针
            int    num1, num2, oper, res;
            char   ch;
            string keepNum = "";

            while (true)
            {
                //扫描每一个字符
                ch = expression.Substring(index, 1).ToCharArray()[0];
                if (IsOper(ch))
                {
                    //判断当前符号栈是否为空
                    if (!operStack.IsEmpty())
                    {
                        if (Priority(ch) <= Priority(operStack.Peek()))
                        {
                            //从数栈中pop两个数,从符号栈中pop出一个符号,计算;
                            //将计算结果放入数栈,将当前符号栈如栈
                            num1 = numStack.Pop();
                            num2 = numStack.Pop();
                            oper = operStack.Pop();
                            res  = Calc(num1, num2, oper);
                            numStack.Push(res);
                            operStack.Push(ch);
                        }
                        else
                        {
                            //入栈
                            operStack.Push(ch);
                        }
                    }
                    else
                    {
                        //为空直接入栈
                        operStack.Push(ch);
                    }
                }
                else
                {
                    //如果是数字,直接入栈
                    //numStack.Push(ch-48);
                    //如果是多位数的话,不能直接入栈

                    keepNum += ch;//先不着急入栈,先存一下
                    if (index == expression.Length - 1)
                    {
                        //如果ch已经是最后一个了,就不要往后看了
                        numStack.Push(ch - 48);
                    }
                    else
                    {
                        //继续扫描,往后看一位,直到后面的是运算符,拼接后入栈
                        if (IsOper(expression.Substring(index + 1, 1).ToCharArray()[0]))
                        {
                            //如果后一位是运算符,入栈
                            numStack.Push(Convert.ToInt32(keepNum));
                            keepNum = "";
                        }
                    }
                }
                index++;
                if (index >= expression.Length)
                {
                    break;
                }
            }

            //扫描完后,依次弹出计算
            while (true)
            {
                //如果符号栈为空,数栈中的就是结果
                if (operStack.IsEmpty())
                {
                    break;
                }
                num1 = numStack.Pop();
                num2 = numStack.Pop();
                oper = operStack.Pop();
                res  = Calc(num1, num2, oper);
                numStack.Push(res);
            }

            res = numStack.Pop();

            Console.WriteLine("计算结果为" + res.ToString());
        }