Exemple #1
0
        public void PrintInOrderIteratively()
        {
            if (Tree == null)
            {
                Console.WriteLine("The tree is empty!");
                return;
            }

            var stack = new MyStack <MyBinaryTreeNode <T> >();
            var node  = Tree.Root;

            stack.Push(node);
            while (!stack.Empty())
            {
                while (node.LeftNode != null)
                {
                    node = node.LeftNode;
                    stack.Push(node);
                }

                while (!stack.Empty())
                {
                    node = stack.Pop();
                    Console.WriteLine(node.Data);

                    if (node.RightNode != null)
                    {
                        node = node.RightNode;
                        stack.Push(node);
                        break;
                    }
                }
            }
        }
        public void ConvertDecimalToBaseTest()
        {
            MyStack<string> result = new MyStack<string>();
            MathExtension.ConvertDecimalToBase(ref result, 100, 5);

            Assert.IsFalse(result.Empty());
        }
        public void PushTest()
        {
            MyStack<int> stack = new MyStack<int>();
            stack.Push(10);

            Assert.IsFalse(stack.Empty());
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var     orderCount = Console.ReadLine();
            MyStack myStack    = new MyStack();

            for (int i = 0, count = Convert.ToInt32(orderCount); i < count; ++i)
            {
                var input = Console.ReadLine().Split(' ');
                switch (input[0])
                {
                case "push":
                    myStack.Push(Convert.ToInt32(input[1]));
                    break;

                case "pop":
                    Console.WriteLine(myStack.Pop());
                    break;

                case "top":
                    Console.WriteLine(myStack.Top());
                    break;

                case "size":
                    Console.WriteLine(myStack.Size());
                    break;

                case "empty":
                    Console.WriteLine(myStack.Empty());
                    break;
                }
            }
        }
Exemple #5
0
        public void Given_stack_When_push_item_Then_empty_false()
        {
            MyStack stack = new MyStack();

            stack.Push(1);
            Assert.IsFalse(stack.Empty());
        }
        public void PopTest()
        {
            MyStack<int> stack = new MyStack<int>();
            stack.Push(10);

            Assert.IsTrue(stack.Pop() == 10, "10");
            Assert.IsTrue(stack.Empty());
        }
        public void Test()
        {
            var sut = new MyStack();

            sut.Push(1);
            sut.Push(2);
            Assert.AreEqual(2, sut.Top());
            Assert.AreEqual(2, sut.Pop());
            Assert.IsFalse(sut.Empty());
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            MyStack obj = new MyStack();

            obj.Push(1);
            obj.Push(2);
            int  param_2 = obj.Pop();
            int  param_3 = obj.Top();
            bool param_4 = obj.Empty();
        }
Exemple #9
0
        public void TestMyStack()
        {
            MyStack stack = new MyStack();

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

            Assert.AreEqual(stack.Pop(), 3);
            Assert.AreEqual(stack.Top(), 2);
            Assert.AreEqual(stack.Empty(), false);
        }
        //[InlineData(1, 1)]
        //[InlineData(2, 2)]
        public void 用队列实现栈()
        {
            MyStack obj = new MyStack();

            obj.Push(1);
            obj.Push(2);
            int  param_2 = obj.Pop();
            int  param_3 = obj.Top();
            bool param_4 = obj.Empty();

            param_2.ShouldBe(2);
            param_3.ShouldBe(1);
            //param_4.ShouldBeTrue();
        }
        public void TestMyStack()
        {
            var testObj = new MyStack();

            testObj.Push(0);
            testObj.Push(1);
            testObj.Push(2);

            Assert.IsFalse(testObj.Empty());
            Assert.AreEqual(2, testObj.Pop());

            testObj.Push(3);
            Assert.AreEqual(3, testObj.Pop());
            Assert.AreEqual(1, testObj.Pop());
            Assert.AreEqual(0, testObj.Pop());
        }
        public void DFTraversal <T>(MyGraphNode <T> root)
        {
            var visitedSet = new HashSet <MyGraphNode <T> >();
            var stack      = new MyStack <MyGraphNode <T> >();
            var vertex     = root;

            stack.Push(root);
            visitedSet.Add(root);

            while (!stack.Empty())
            {
                vertex = stack.Pop();
                Console.WriteLine(vertex.Data);

                foreach (MyGraphNode <T> node in vertex.Neighbors)
                {
                    if (!visitedSet.Contains(node))
                    {
                        visitedSet.Add(node);
                        stack.Push(node);
                    }
                }
            }
        }
Exemple #13
0
        public String Test(String input1, String input2)
        {
            var actions    = JsonConvert.DeserializeObject <String[]>(input1);
            var parameters = JsonConvert.DeserializeObject <Int32[][]>(input2);

            var result = new List <Object>();

            MyStack stack = null;

            Console.WriteLine("MyStack stack = null;");
            for (Int32 i = 0; i < actions.Length; i++)
            {
                String action = actions[i];
                switch (action)
                {
                case "MyStack":
                    Console.WriteLine("stack = new MyStack();");
                    stack = new MyStack();
                    result.Add(null);
                    break;

                case "push":
                    Console.Write("stack.push({0});", parameters[i][0]);
                    stack?.Push(x: parameters[i][0]);
                    if (stack != null)
                    {
                        Console.WriteLine("\t\t// the stack is [{0}] <- top", String.Join(",", stack));
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    result.Add(null);
                    break;

                case "pop":
                    Console.Write("stack.Pop();");
                    Int32?pop = stack?.Pop();
                    if (pop != null)
                    {
                        Console.Write("\t\t// returns: {0}", pop);
                    }
                    if (stack != null)
                    {
                        Console.WriteLine(" - the stack is [{0}] <- top", String.Join(",", stack));
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    result.Add(pop);
                    break;

                case "top":
                    Console.Write("stack.Top();");
                    Int32?top = stack?.Top();
                    if (top != null)
                    {
                        Console.WriteLine("\t\t// returns: {0}", top);
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    result.Add(top);
                    break;

                case "empty":
                    Console.Write("stack.Empty();");
                    Boolean?empty = stack?.Empty();
                    if (empty != null)
                    {
                        Console.WriteLine("\t\t// returns: {0}", empty);
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    result.Add(empty);
                    break;
                }
            }

            return(JsonConvert.SerializeObject(result));
        }
Exemple #14
0
        public void Given_stack_When_no_items_Then_empty_true()
        {
            MyStack stack = new MyStack();

            Assert.IsTrue(stack.Empty());
        }
Exemple #15
0
 public void EmptyTest()
 {
     Assert.True(obj.Empty() == false);
 }
Exemple #16
0
        public static void ConvertDecimalToBase(ref MyStack<string> result, int num, int @base)
        {
            if (@base > 16)
            {
                throw new ArgumentException("base overstep 16");
            }

            if (result == null || !result.Empty())
            {
                throw new ArgumentException("result is null or not empty");
            }

            string[] items = new[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "A", "B", "C", "D", "E", "F"};

            while (num > 0)
            {
                result.Push(items[num % @base]);
                num /= @base;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lElements"></param>
        /// <param name="rElements"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool ElementsMatching(string[] lElements, string[] rElements, string content)
        {
            if (lElements.Count() != rElements.Count())
            {
                throw new ArgumentException("the element pairs count is not matching");
            }

            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }

            MyStack<int> stack = new MyStack<int>();
            while (content.Length > 0)
            {
                int rIndex;
                int lIndex = rIndex = content.Length;
                int element1 = -1;
                int element2 = -1;
                for (int i = 0; i < lElements.Length; i++)
                {
                    int index1 = content.IndexOf(lElements[i], StringComparison.Ordinal);
                    if (index1 > -1 && index1 < lIndex)
                    {
                        lIndex = index1;
                        element1 = i;
                        if (lIndex == 0)
                        {
                            break;
                        }
                    }
                    int index2 = content.IndexOf(rElements[i], StringComparison.Ordinal);
                    if (index2 > -1 && index2 < rIndex)
                    {
                        rIndex = index2;
                        element2 = i;
                        if (rIndex == 0)
                        {
                            break;
                        }
                    }
                }

                if (element1 == -1 && element2 == -1)
                {
                    return stack.Empty();
                }

                if (lIndex < rIndex)
                {
                    //push
                    stack.Push(element1);
                    content = content.Substring(lIndex + lElements[element1].Length);
                }
                else if (lIndex > rIndex)
                {
                    //pop
                    if (stack.Empty() || stack.Top() != element2)
                    {
                        return false;
                    }
                    stack.Pop();
                    content = content.Substring(rIndex + rElements[element2].Length);
                }
                else
                {
                    return stack.Empty();
                }
            }

            return stack.Empty();
        }