コード例 #1
0
ファイル: StackLinkList.cs プロジェクト: basantvip/MyProjects
        public static void Demo()
        {
            var stack = new StackLinkList <object>();

            //Console.WriteLine(stack.Peek());
            //Console.WriteLine(stack.Pop());
            stack.Push(1);
            stack.Push("Basant");
            stack.Push(3);
            stack.Push(4);
            stack.PrintStackItems();
            Console.WriteLine("printing reverse");
            stack.PrintStackItemsReverse();
            Console.WriteLine("printing reverse completed");
            Console.WriteLine(stack.Peek());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            stack.Push(10);
            stack.Push(21);
            stack.Push(31);
            Console.WriteLine(stack.Pop());
            Console.WriteLine($"Count: {stack.Count()}");
            stack.Clear();
            Console.WriteLine(stack.Pop());
        }
コード例 #2
0
ファイル: Postfix.cs プロジェクト: basantvip/MyProjects
        public static void Demo()
        {
            var stack = new StackLinkList <string>();

            while (true)
            {
                Console.Write("\nEnter Space Separated Postfix String (0 or Enter to exit): ");
                var postfixString = Console.ReadLine() ?? "0";
                if (postfixString.Trim() == "0" || postfixString.Trim() == "")
                {
                    return;
                }
                var postfixList = postfixString.Split(' ');

                var validString = true;

                foreach (var token in postfixList)
                {
                    switch (GetTokenType(token))
                    {
                    case "Operand":
                        stack.Push(token);
                        break;

                    case "Operator":
                        var op2 = stack.Pop();
                        var op1 = stack.Pop();
                        stack.Push(Apply(op1, op2, token).ToString());
                        break;

                    case "Invalid Token":
                        Console.WriteLine("Invalid character in postfix string");
                        validString = false;
                        break;
                    }
                }
                if (validString)
                {
                    Console.WriteLine($"Result: {stack.Pop()}");
                }
            }
        }