Esempio n. 1
0
        public void TestLinkedInts()
        {
            dynamic stack = new LinkedStack <int>();

            var test1 = 1;
            var test2 = 2;
            var test3 = 3;

            stack.Push(test1);
            stack.Push(test2);
            stack.Push(test3);

            Assert.AreEqual(3, stack.Size);

            Assert.AreEqual(test3, stack.Pop());
            Assert.AreEqual(test2, stack.Pop());
            Assert.AreEqual(test1, stack.Pop());
        }
Esempio n. 2
0
        public void TestLinkedStrings()
        {
            dynamic stack = new LinkedStack <string>();

            var test1 = "Hi stack.";
            var test2 = "Hi again, stack";
            var test3 = "How's it going, stack?";

            stack.Push(test1);
            stack.Push(test2);
            stack.Push(test3);

            Assert.AreEqual(3, stack.Size);

            Assert.AreEqual(test3, stack.Pop());
            Assert.AreEqual(test2, stack.Pop());
            Assert.AreEqual(test1, stack.Pop());
        }
Esempio n. 3
0
        public static bool CheckIfBalanced(string parString)
        {
            if (parString.Length == 0)
            {
                return(true);
            }
            var stack = new LinkedStack <char>();

            foreach (char c in parString)
            {
                switch (c)
                {
                case LEFT_PAREN:
                case LEFT_SQUARE:
                case LEFT_CURLY:
                    stack.Push(c);
                    break;

                case RIGHT_PAREN:
                    if (stack.Size == 0 || stack.Pop() != LEFT_PAREN)
                    {
                        return(false);
                    }
                    break;

                case RIGHT_SQUARE:
                    if (stack.Size == 0 || stack.Pop() != LEFT_SQUARE)
                    {
                        return(false);
                    }
                    break;

                case RIGHT_CURLY:
                    if (stack.Size == 0 || stack.Pop() != LEFT_CURLY)
                    {
                        return(false);
                    }
                    break;
                }
            }
            return(true);
        }