Exemple #1
0
        public void DoublyLinkedListAddTest()
        {
            DoublyLinkedList list = new SimpleDLL();

            list.Add(5);
            var head = list.Head();

            Assert.IsNotNull(head);
            Assert.AreEqual(5, head.Data);
            Assert.IsNull(head.Next.Next);
            Assert.IsNull(head.Previous);

            list.Add(7);
            head = list.Head();
            Assert.AreEqual(5, head.Data);
            var currentNode = list.GetNext();

            Assert.AreEqual(7, currentNode.Data);

            list.Add(9);
            head = list.Head();
            Assert.AreEqual(5, head.Data);
            currentNode = list.GetNext();
            Assert.AreEqual(7, currentNode.Data);
            currentNode = list.GetNext();
            Assert.AreEqual(9, currentNode.Data);
            Assert.IsNull(currentNode.Next);

            list.Add(11);
            currentNode = list.GetCurrentNode();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(9, currentNode.Data);
            currentNode = list.GetNext();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(11, currentNode.Data);
            var tail = list.Tail();

            Assert.IsNotNull(tail);
            Assert.IsNotNull(tail.Previous);
            Assert.AreEqual(11, tail.Data);
        }
Exemple #2
0
        public void DoublyLinkedListTraverseTest()
        {
            var list = new SimpleDLL();

            list.Add(5);
            list.Add(7);
            list.Add(9);
            list.Add(11);

            var head = list.Head();

            Assert.AreEqual(5, head.Data);
            var currentNode = list.GetNext();

            Assert.AreEqual(7, currentNode.Data);
            currentNode = list.GetNext();
            Assert.AreEqual(9, currentNode.Data);
            currentNode = list.GetNext();
            Assert.AreEqual(11, currentNode.Data);
            Assert.IsNull(currentNode.Next);
            Assert.AreEqual(currentNode, list.Tail());
            currentNode = list.GetNext();
            Assert.IsNotNull(currentNode);
            Assert.IsNull(currentNode.Next);
            Assert.AreEqual(11, currentNode.Data);

            currentNode = list.GetPreviousNode();
            Assert.AreEqual(9, currentNode.Data);
            currentNode = list.GetPreviousNode();
            Assert.AreEqual(7, currentNode.Data);
            currentNode = list.GetPreviousNode();
            Assert.AreEqual(5, currentNode.Data);
            Assert.AreEqual(currentNode, list.Head());
            Assert.IsNull(currentNode.Previous);
            currentNode = list.GetPreviousNode();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(5, currentNode.Data);
        }
Exemple #3
0
        public void DoublyLinkedListBasicTest()
        {
            DoublyLinkedList list = new SimpleDLL();

            list.Add(5);
            var head = list.Head();

            Assert.IsNotNull(head);
            Assert.AreEqual(5, head.Data);
            Assert.IsNull(head.Previous);

            var tail = list.Tail();

            Assert.IsNotNull(tail);
            Assert.IsNull(tail.Data);
            Assert.IsNotNull(tail.Previous);
            Assert.AreEqual(head.Next, tail);
            Assert.AreEqual(tail.Previous, head);
            Assert.IsTrue(head.Next.Equals(tail));
            Assert.IsTrue(tail.Previous.Equals(head));
        }
Exemple #4
0
        public void DoublyLinkedListRemoveNodesTest()
        {
            var list = new SimpleDLL();

            list.Add(5);
            list.Add(7);
            list.Add(9);

            //Remove in middle of list
            var head = list.Head();

            Assert.IsNotNull(head);
            Assert.AreEqual(5, head.Data);
            var currentNode = list.GetNext();

            Assert.IsNotNull(currentNode);
            Assert.AreEqual(7, currentNode.Data);
            list.RemoveCurrentNode();
            currentNode = list.GetCurrentNode();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(9, currentNode.Data);
            Assert.IsNull(currentNode.Next);
            Assert.AreEqual(currentNode.Previous, list.Head());

            //remove tail node
            list.Add(11);   //5 -> 9 -> 11
            var tail = list.Tail();

            while (list.GetCurrentNode() != tail)   //move currentNode to tail
            {
                currentNode = list.GetNext();
            }
            list.RemoveCurrentNode();
            tail        = list.Tail();
            currentNode = list.GetCurrentNode();
            Assert.IsNotNull(tail);
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(9, currentNode.Data);
            Assert.AreEqual(9, tail.Data);
            Assert.AreEqual(currentNode, tail);

            //remove head node
            list.Add(13);                          //5 -> 9 -> 13
            head = list.Head();
            while (list.GetPreviousNode() != head) //move currentNode to head
            {
                currentNode = list.GetPreviousNode();
            }
            currentNode = list.GetCurrentNode();
            Assert.AreEqual(5, currentNode.Data);
            Assert.AreEqual(5, head.Data);
            Assert.AreEqual(currentNode, head);
            list.RemoveCurrentNode();
            currentNode = list.GetCurrentNode();
            head        = list.Head();
            Assert.IsNotNull(currentNode);
            Assert.IsNotNull(head);
            Assert.AreEqual(9, currentNode.Data);
            Assert.AreEqual(9, head.Data);
            Assert.AreEqual(currentNode, head);
        }