Exemplo n.º 1
0
        public SimpleLinkedList <int> Sum(SimpleLinkedList <int> list1, SimpleLinkedList <int> list2)
        {
            SimpleLinkedList <int> resultList = new SimpleLinkedList <int>();
            var n1    = list1.Head;
            var n2    = list2.Head;
            int carry = 0;

            while (n1 != null || n2 != null)
            {
                int op1 = 0, op2 = 0;
                if (n1 != null)
                {
                    op1 = n1.Value;
                }

                if (n2 != null)
                {
                    op2 = n2.Value;
                }

                int rez = op1 + op2 + carry;

                resultList.AddLast(rez % 10);
                carry = rez / 10;

                n1 = n1?.Next;
                n2 = n2?.Next;
            }

            if (carry == 1)
            {
                resultList.AddLast(1);
            }
            return(resultList);
        }
            public void AddLastMethod_AddsNewItemAtTheTailOfList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();

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

                Assert.ReferenceEquals(linkedList.Last, "new Item 2");
                Assert.True(linkedList.Last.Item == "new Item 2");
            }
            public void AddLastMethod_AddsNewItemAtTheTailOfList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

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

                Assert.ReferenceEquals(linkedList.Last, "new Item 2");
                Assert.True(linkedList.Last.Item == "new Item 2");
            }
            public void RemoveMethod_SetsThe_LinkProperty_Of_LastProperty_ToNull()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

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

                linkedList.Remove("new Item 2");

                Assert.Null(linkedList.Last.Link);
            }
            public void RemoveMethod_ReturnsTrueIfItemToBeRemovedIsFound()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

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

                bool result = linkedList.Remove("asdf");

                Assert.True(result);
            }
            public void RemoveLastMethod_RemovesTheLastItemInTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

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

                linkedList.RemoveLast();

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

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

                linkedList.RemoveLast();

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

                linkedList.AddLast("asdf"); expectedCount++;
                linkedList.AddLast("qwer"); expectedCount++;
                linkedList.AddLast("zxcv"); expectedCount++;

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

                linkedList.AddLast("new Item 1");
                LinkedListNode <string> previousTail = linkedList.Last;

                linkedList.AddLast("new Item 2");
                LinkedListNode <string> currentTail = linkedList.Last;

                Assert.ReferenceEquals(previousTail.Link, currentTail);
                Assert.Equal("new Item 2", previousTail.Link.Item);
            }
            public void RemoveMethod_DecrementsLengthOfList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();
                int expectedCount = 0;

                linkedList.AddLast("asdf"); expectedCount++;
                linkedList.AddLast("qwer"); expectedCount++;
                linkedList.AddLast("zxcv"); expectedCount++;

                linkedList.Remove("asdf"); expectedCount--;

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

                linkedList.AddLast("asdf");

                Assert.True(linkedList.Count == 1);
            }
            public void IsEmpty_ReturnsFalseIfListIsNotEmpty()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddLast("asdf");

                Assert.False(linkedList.IsEmpty);
            }
            public void AddLastMethod_IncrementsLengthOfList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddLast("asdf");

                Assert.True(linkedList.Count == 1);
            }
            public void RemoveLastMethod_RemovesTheOnlyItemInTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddLast("asdf");

                linkedList.RemoveLast();

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

                linkedList.AddLast("new Item 1");
                LinkedListNode<string> previousTail = linkedList.Last;

                linkedList.AddLast("new Item 2");
                LinkedListNode<string> currentTail = linkedList.Last;

                Assert.ReferenceEquals(previousTail.Link, currentTail);
                Assert.Equal("new Item 2", previousTail.Link.Item);
            }
            public void RemoveLastMethod_SetsThe_LastProperty_ToNullIfTheItemToBeRemovedIsTheOnlyItemInTheList()
            {
                SimpleLinkedList <string> linkedList = new SimpleLinkedList <string>();

                linkedList.AddLast("new Item 1");

                linkedList.RemoveLast();

                // acessing linkedList.Last throws exception if the tail of the list is null
                Assert.Throws <InvalidOperationException>(
                    delegate
                {
                    LinkedListNode <string> node = linkedList.Last;
                });
            }
            public void CountProperty_ReturnsAccurateCount()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                int expectedCount = 0;
                linkedList.AddLast("asdf"); expectedCount++;
                linkedList.AddLast("qwer"); expectedCount++;
                linkedList.AddLast("zxcv"); expectedCount++;

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

                linkedList.RemoveLast();

                Assert.NotEqual("qwer", linkedList.Last.Item);
            }
            public void RemoveLastMethod_RemovesTheOnlyItemInTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddLast("asdf");

                linkedList.RemoveLast();

                Assert.Null(linkedList.Find("asdf"));
            }
            public void RemoveMethod_DecrementsLengthOfList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                int expectedCount = 0;
                linkedList.AddLast("asdf"); expectedCount++;
                linkedList.AddLast("qwer"); expectedCount++;
                linkedList.AddLast("zxcv"); expectedCount++;

                linkedList.Remove("asdf"); expectedCount--;

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

                bool result = linkedList.Remove("asdf");

                Assert.True(result);
            }
            public void RemoveMethod_SetsThe_LastProperty_ToNullIfTheItemToBeRemovedIsTheOnlyItemInTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddLast("new Item 1");

                linkedList.Remove("new Item 1");

                // acessing linkedList.Last throws exception if the tail of the list is null
                Assert.Throws<InvalidOperationException>(
                    delegate
                    {
                        LinkedListNode<string> node = linkedList.Last;
                    });
            }
            public void RemoveMethod_SetsThe_LastProperty_ToTheSecondToLastNodeIfTheItemToBeRemovedIsTheTailOfTheList()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddLast("new Item 1");
                linkedList.AddLast("new Item 2");

                linkedList.Remove("new Item 2");

                Assert.Equal("new Item 1", linkedList.Last.Item);
            }
            public void RemoveMethod_SetsThe_LinkProperty_Of_LastProperty_ToNull()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddLast("new Item 1");
                linkedList.AddLast("new Item 2");

                linkedList.Remove("new Item 2");

                Assert.Null(linkedList.Last.Link);
            }
            public void IsEmpty_ReturnsFalseIfListIsNotEmpty()
            {
                SimpleLinkedList<string> linkedList = new SimpleLinkedList<string>();
                linkedList.AddLast("asdf");

                Assert.False(linkedList.IsEmpty);
            }