public void LastProperty_ReturnsTheCorrectNodeInListWithMultipleItems()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");
                linkedList.AddFirst("zxcv");

                Assert.Equal("asdf", linkedList.Last.Item);
            }
            public void AddFirstMethod_SetsThe_LinkProperty_OfTheCurrentHeadToThePreviousHeadOfTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();

                linkedList.AddFirst("new Item 1");
                linkedList.AddFirst("new Item 2");

                Assert.ReferenceEquals(linkedList.First.Link, "new Item 1");
                Assert.Equal("new Item 1", linkedList.First.Link.Item);
            }
            public void AddFirstMethod_SetsThe_LinkProperty_OfTheCurrentHeadToThePreviousHeadOfTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("new Item 1");
                linkedList.AddFirst("new Item 2");

                Assert.ReferenceEquals(linkedList.First.Link, "new Item 1");
                Assert.Equal("new Item 1", linkedList.First.Link.Item);
            }
            public void FindMethod_ReturnsNullIfItemIsNotFound()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");

                LinkedListNode <string> node = linkedList.Find("zzzzzzzzzzzz");

                Assert.Null(node);
            }
            public void RemoveFirstMethod_RemovesTheFirstItemInTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");

                linkedList.RemoveFirst();

                Assert.NotEqual("qwer", linkedList.First.Item);
            }
            public void RemoveFirstMethod_SetsThe_FirstProperty_ToTheSecondNodeIfTheItemToBeRemovedIsTheHeadOfTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("new Item 1");
                linkedList.AddFirst("new Item 2");

                linkedList.RemoveFirst();

                Assert.Equal("new Item 1", linkedList.First.Item);
            }
            public void RemoveMethod_RemovesCorrectItemFromListWithMultipleItems()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");
                linkedList.AddFirst("middle");
                linkedList.AddFirst("qwer");

                linkedList.Remove("middle");

                Assert.Null(linkedList.Find("middle"));
            }
            public void RemoveLastMethod_DecrementsLengthOfList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();
                int expectedCount = 0;

                linkedList.AddFirst("asdf"); expectedCount++;
                linkedList.AddFirst("qwer"); expectedCount++;

                linkedList.RemoveLast(); expectedCount--;

                Assert.Equal(expectedCount, linkedList.Count);
            }
            public void FindMethod_ReturnsCorrectNode()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");

                LinkedListNode <string> node = linkedList.Find("asdf");

                Assert.NotNull(node);
                Assert.Equal("asdf", node.Item);
            }
            public void LastProperty_ReturnsTheOnlyNodeInTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");

                Assert.Equal("asdf", linkedList.Last.Item);
            }
            public void AddFirstMethod_IncrementsLengthOfList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                int expectedCount = 0;

                linkedList.AddFirst("asdf"); expectedCount++;

                Assert.Equal(expectedCount, linkedList.Count);
            }
            public void AddFirstMethod_AddsNewItemAtTheHeadOfList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();

                linkedList.AddFirst("new Item 1");

                Assert.ReferenceEquals(linkedList.First, "new Item 1");
                Assert.Equal("new Item 1", linkedList.First.Item);
            }
Exemplo n.º 13
0
        public SimpleLinkedList <int> SumReverse(SimpleLinkedList <int> list1, SimpleLinkedList <int> list2)
        {
            SimpleLinkedList <int> resultList = new SimpleLinkedList <int>();

            EqualizeLength(list1, list2);
            var n1 = list1.Head;
            var n2 = list2.Head;

            PartialSum result = BuildSum(n1, n2);

            resultList.AddFirst(result.Node.Value);
            if (result.Carry > 0)
            {
                resultList.AddFirst(1);
            }

            return(resultList);
        }
            public void AddFirstMethod_IncrementsLengthOfList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();
                int expectedCount = 0;

                linkedList.AddFirst("asdf"); expectedCount++;

                Assert.Equal(expectedCount, linkedList.Count);
            }
            public void AddFirstMethod_AddsNewItemAtTheHeadOfList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("new Item 1");

                Assert.ReferenceEquals(linkedList.First, "new Item 1");
                Assert.Equal("new Item 1", linkedList.First.Item);
            }
            public void RemoveFirstMethod_RemovesTheOnlyItemFromList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("asdf");

                linkedList.RemoveFirst();

                Assert.Null(linkedList.Find("asdf"));
            }
Exemplo n.º 17
0
        public void CheckListImplementation()
        {
            var        myList  = new SimpleLinkedList <int>();
            List <int> netList = new List <int>();

            myList.AddFirst(1);
            foreach (var i in myList)
            {
                netList.Add(i);
            }
            LinkedList <int> nwtList = new LinkedList <int>();

            Assert.Equal(1, netList[0]);

            myList.AddFirst(0);
            Assert.Equal(0, myList.Head.Value);
            Assert.Equal(1, myList.Last.Value);

            SimpleLinkedList <int> my3 = new SimpleLinkedList <int>(new[] { 1, 2, 3 });

            var node2 = my3.Find(2);

            my3.Remove(node2);

            netList.Clear();
            foreach (var i in my3)
            {
                netList.Add(i);
            }

            Assert.Equal(1, netList[0]);
            Assert.Equal(3, netList[1]);

            my3.Remove(my3.Head);

            netList.Clear();
            foreach (var i in my3)
            {
                netList.Add(i);
            }
            Assert.Equal(3, netList[0]);
        }
            public void RemoveLastMethod_SetsThe_FirstProperty_ToNullIfTheItemToBeRemovedIsTheOnlyItemInTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddFirst("new Item 1");

                linkedList.RemoveLast();

                // acessing linkedList.First throws exception if the head of the list is null
                Assert.Throws <InvalidOperationException>(
                    delegate
                {
                    LinkedListNode <string> node = linkedList.First;
                });
            }
Exemplo n.º 19
0
        private void EqualizeLength(SimpleLinkedList <int> list1, SimpleLinkedList <int> list2)
        {
            int length1 = 0;
            var n1      = list1.Head;

            while (n1 != null)
            {
                length1++;
                n1 = n1.Next;
            }

            int length2 = 0;
            var n2      = list2.Head;

            while (n2 != null)
            {
                length2++;
                n2 = n2.Next;
            }

            if (length1 > length2)
            {
                while (length1 > length2)
                {
                    list2.AddFirst(0);
                    length2++;
                }

                return;
            }

            if (length2 > length1)
            {
                while (length2 > length1)
                {
                    list1.AddFirst(0);
                    length1++;
                }
            }
        }
            public void LastProperty_ReturnsTheCorrectNodeInListWithMultipleItems()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");
                linkedList.AddFirst("zxcv");

                Assert.Equal("asdf", linkedList.Last.Item);
            }
            public void LastProperty_ReturnsTheOnlyNodeInTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");

                Assert.Equal("asdf", linkedList.Last.Item);
            }
            public void RemoveFirstMethod_RemovesTheFirstItemInTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");

                linkedList.RemoveFirst();

                Assert.NotEqual("qwer", linkedList.First.Item);
            }
            public void RemoveLastMethod_DecrementsLengthOfList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                int expectedCount = 0;
                linkedList.AddFirst("asdf"); expectedCount++;
                linkedList.AddFirst("qwer"); expectedCount++;

                linkedList.RemoveLast(); expectedCount--;

                Assert.Equal(expectedCount, linkedList.Count);
            }
            public void RemoveMethod_RemovesCorrectItemFromListWithMultipleItems()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");
                linkedList.AddFirst("middle");
                linkedList.AddFirst("qwer");

                linkedList.Remove("middle");

                Assert.Null(linkedList.Find("middle"));
            }
            public void RemoveMethod_RemovesTheOnlyItemFromList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");

                linkedList.Remove("asdf");

                Assert.Null(linkedList.Find("asdf"));
            }
            public void RemoveMethod_SetsThe_FirstProperty_ToNullIfTheItemToBeRemovedIsTheOnlyItemInTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("new Item 1");

                linkedList.Remove("new Item 1");
                                // Assert
                // acessing linkedList.First throws exception if the head of the list is null
                Assert.Throws<InvalidOperationException>(
                    delegate
                    {
                        LinkedListNode<string> node = linkedList.First;
                    });
            }
            public void RemoveMethod_SetsThe_FirstProperty_ToTheSecondNodeIfTheItemToBeRemovedIsTheHeadOfTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("new Item 1");
                linkedList.AddFirst("new Item 2");

                linkedList.Remove("new Item 2");

                Assert.Equal("new Item 1", linkedList.First.Item);
            }
            public void FindMethod_ReturnsNullIfItemIsNotFound()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");

                LinkedListNode<string> node = linkedList.Find("zzzzzzzzzzzz");

                Assert.Null(node);
            }
Exemplo n.º 29
0
        static void Main(string[] args)
        {
            #region Simple Linked
            //var firstNode = new SimpleLinkedNode<string>();
            //firstNode.Data = "primeiro";

            //var secondNode = new SimpleLinkedNode<string>();
            //secondNode.Data = "segundo";

            //var thirdNode = new SimpleLinkedNode<string>();
            //thirdNode.Data = "terceiro";

            //var fourthNode = new SimpleLinkedNode<string>();
            //fourthNode.Data = "quarto";

            //var fifthNode = new SimpleLinkedNode<string>();
            //fifthNode.Data = "quinto";

            //var sixthNode = new SimpleLinkedNode<string>();
            //sixthNode.Data = "sexto";

            //var seventhNode = new SimpleLinkedNode<string>();
            //seventhNode.Data = "quarto";

            //firstNode.Next = secondNode;
            //secondNode.Next = thirdNode;
            //thirdNode.Next = fourthNode;
            //fourthNode.Next = fifthNode;
            //fifthNode.Next = sixthNode;
            //sixthNode.Next = seventhNode;

            //var currentNode = firstNode;
            //while (currentNode != null)
            //{
            //    Console.WriteLine(currentNode.Data);
            //    currentNode = currentNode.Next;
            //}
            #endregion

            #region Circular
            //var firstNode = new SimpleLinkedNode<string>();
            //firstNode.Data = "primeiro";

            //var secondNode = new SimpleLinkedNode<string>();
            //secondNode.Data = "segundo";

            //var thirdNode = new SimpleLinkedNode<string>();
            //thirdNode.Data = "terceiro";

            //var fourthNode = new SimpleLinkedNode<string>();
            //fourthNode.Data = "quarto";

            //var fifthNode = new SimpleLinkedNode<string>();
            //fifthNode.Data = "quinto";

            //var sixthNode = new SimpleLinkedNode<string>();
            //sixthNode.Data = "sexto";



            //firstNode.Next = secondNode;
            //secondNode.Next = thirdNode;
            //thirdNode.Next = fourthNode;
            //fourthNode.Next = fifthNode;
            //fifthNode.Next = sixthNode;
            //sixthNode.Next = firstNode;

            //var currentNode = firstNode;
            //var loop = 0;
            //while (loop<3)
            //{
            //    if(currentNode == firstNode) Console.WriteLine("\nLoop #" + (loop+1));
            //    Console.WriteLine(currentNode.Data);
            //    if (currentNode == sixthNode) loop++;
            //    currentNode = currentNode.Next;
            //}
            #endregion

            #region Back And Forth
            //var firstNode = new DoubleLinkedNode<string>();
            //firstNode.Data = "primeiro";

            //var secondNode = new DoubleLinkedNode<string>();
            //secondNode.Data = "segundo";

            //var thirdNode = new DoubleLinkedNode<string>();
            //thirdNode.Data = "terceiro";

            //var fourthNode = new DoubleLinkedNode<string>();
            //fourthNode.Data = "quarto";

            //var fifthNode = new DoubleLinkedNode<string>();
            //fifthNode.Data = "quinto";

            //var sixthNode = new DoubleLinkedNode<string>();
            //sixthNode.Data = "sexto";

            //firstNode.Next = secondNode;
            //secondNode.Next = thirdNode;
            //thirdNode.Next = fourthNode;
            //fourthNode.Next = fifthNode;
            //fifthNode.Next = sixthNode;

            //secondNode.Previous = firstNode;
            //thirdNode.Previous = secondNode;
            //fourthNode.Previous = thirdNode;
            //fifthNode.Previous = fourthNode;
            //sixthNode.Previous = fifthNode;

            //var currentNode = firstNode;

            //var goBack = false;
            //var loop = 0;
            //while (currentNode != null)
            //{
            //    if (currentNode == firstNode && goBack && loop < 2)
            //    {
            //        goBack = false;
            //        loop++;
            //    }
            //    if (currentNode == firstNode) Console.WriteLine("\nLööp #" + (loop + 1));
            //    Console.WriteLine(currentNode.Data);
            //    if (currentNode == sixthNode) goBack = true;

            //    if (goBack) currentNode = currentNode.Previous;
            //    else currentNode = currentNode.Next;
            //}
            #endregion

            var list = new SimpleLinkedList <string>("First");
            list.Add("Second");
            list.Add("Third");
            list.Add("Forth");
            list.Add("Fifth");
            list.Add("Sixth");
            list.Add("Seventh");
            list.Add("Eighth");
            list.Add("Seventh");
            list.Add("Nineth");
            list.AddFirst("zero");
            list.Replace("Seventh", "Hello");
        }
            public void FindMethod_ReturnsCorrectNode()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddFirst("asdf");
                linkedList.AddFirst("qwer");

                LinkedListNode<string> node = linkedList.Find("asdf");

                Assert.NotNull(node);
                Assert.Equal("asdf", node.Item);
            }