示例#1
0
        public void Push_Pop_Stack_Test()
        {
            DSStack <int> stack = new DSStack <int>();

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            stack.Push(4);
            stack.Push(5);
            stack.Push(6);

            stack.Pop();

            Assert.AreEqual(5, stack.Head.Data);

            Assert.IsNull(stack.Head.Prev);

            Assert.IsNotNull(stack.Head.Next);

            Assert.IsNotNull(stack.Head.Next.Next);

            Assert.IsNotNull(stack.Head.Next.Next.Next);

            Assert.IsNotNull(stack.Head.Next.Next.Next.Next);

            Assert.IsNull(stack.Head.Next.Next.Next.Next.Next);
        }
        public bool IsPalindrome()
        {
            if (_data == null)
            {
                return(false);
            }

            FindMiddleOfList <char> findMiddle = new FindMiddleOfList <char>(_data);

            int middleIndex = findMiddle.findCenterIndex();

            DSStack <char> stack = new DSStack <char>();

            int index = 0;

            DSNode <char> current = _data;

            while (current != null)
            {
                if (index > middleIndex)
                {
                    var popped = stack.Pop();

                    if (popped != current.Data)
                    {
                        break;
                    }
                }
                else
                {
                    stack.Push(current.Data);
                    if (index == middleIndex && middleIndex % 2 != 0)
                    {
                        stack.Pop();
                    }
                }

                index++;

                current = current.Next;
            }

            return(stack.Count == 0);
        }