Ejemplo n.º 1
0
        public static SinglyLinkedList MergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2)
        {
            SinglyLinkedList resultList = new SinglyLinkedList();

            while (head1 != null && head2 != null)
            {
                if (head1 != null && head1.Data <= head2.Data)
                {
                    resultList.Append(head1.Data);
                    head1 = head1.Next;
                }
                else if (head2 != null)
                {
                    resultList.Append(head2.Data);
                    head2 = head2.Next;
                }
            }

            while (head1 != null)
            {
                resultList.Append(head1.Data);
                head1 = head1.Next;
            }

            while (head2 != null)
            {
                resultList.Append(head2.Data);
                head2 = head2.Next;
            }

            return(resultList);
        }
        public void HappyPath()
        {
            SinglyLinkedList list1 = new SinglyLinkedList();

            list1.Append(1);
            list1.Append(2);
            list1.Append(3);
            list1.Append(4);
            list1.Append(5);

            SinglyLinkedList list2 = new SinglyLinkedList();

            list2.Append(10);
            list2.Append(20);
            list2.Append(30);
            list2.Append(40);
            list2.Append(50);

            SinglyLinkedList list3 = new SinglyLinkedList();

            list3.Head = LLMerge.Program.LLMerge(list1, list2);

            List <int> nodeVals    = new List <int>();
            Node       currentNode = list3.Head;

            while (currentNode != null)
            {
                nodeVals.Add(currentNode.Data);
                currentNode = currentNode.Next;
            }
            int[] nodeValList  = nodeVals.ToArray();
            int[] nodeExpected = new int[] { 1, 10, 2, 20, 3, 30, 4, 40, 5, 50 };
            Assert.Equal(nodeExpected, nodeValList);
        }
        public void AFullBNull()
        {
            SinglyLinkedList list1 = new SinglyLinkedList();
            SinglyLinkedList list2 = new SinglyLinkedList();

            list1.Append(1);
            list1.Append(2);
            list1.Append(3);
            list1.Append(4);
            list1.Append(5);

            SinglyLinkedList list3 = new SinglyLinkedList();

            list3.Head = LLMerge.Program.LLMerge(list1, list2);

            List <int> nodeVals    = new List <int>();
            Node       currentNode = list3.Head;

            while (currentNode != null)
            {
                nodeVals.Add(currentNode.Data);
                currentNode = currentNode.Next;
            }
            int[] nodeValList  = nodeVals.ToArray();
            int[] nodeExpected = new int[] { 1, 2, 3, 4, 5 };
            Assert.Equal(nodeExpected, nodeValList);
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("list1:");
            SinglyLinkedList list1 = new SinglyLinkedList();

            list1.Append(1);
            list1.Append(2);
            list1.Append(3);
            list1.Append(4);
            list1.Append(5);
            list1.ReadThrough();

            Console.WriteLine();
            Console.WriteLine("list2:");
            SinglyLinkedList list2 = new SinglyLinkedList();

            list2.Append(10);
            list2.Append(20);
            list2.Append(30);
            list2.Append(40);
            list2.Append(50);

            list2.ReadThrough();
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("Merge list1 and list2: list3:");
            SinglyLinkedList list3 = new SinglyLinkedList();

            list3.Head = LLMerge(list1, list2);
            list3.ReadThrough();
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            //Linked List
            //This project gives a better understanding to C# linkedlists
            //Node and a Pointer to the next node

            Node firstNode  = new Node(1);
            Node secondNode = new Node(2);

            secondNode.Next = firstNode;

            SinglyLinkedList mySList = new SinglyLinkedList();

            mySList.AddFirst(1);
            mySList.AddFirst(2);
            mySList.AddFirst(3);
            mySList.Append(10);
            mySList.Append(20);
            mySList.Append(30);

            mySList.InsertNode(11, 4);
            mySList.DeleteFirst();
            mySList.DeleteLast();
            mySList.AddFirst(4);
            mySList.AddLast(40);

            mySList.Print();
        }
Ejemplo n.º 6
0
        public void MultipleAppend()
        {
            SinglyLinkedList list = new SinglyLinkedList();

            list.Append(3);
            list.Append(2);
            list.Append(1);
            Assert.Equal(1, list.Head.Next.Next.Data);
        }
 public void SetupTests()
 {
     _list = new SinglyLinkedList <string>();
     _list.Append("one");
     _list.Append("two");
     _list.Append("three");
     _list.Append("four");
     _list.Append("five");
 }
        void AppendItems()
        {
            singlyLinkedWords.Append("hello");
            singlyLinkedWords.Append("world");

            Assert.Equal("hello", singlyLinkedWords.First);
            Assert.Equal("world", singlyLinkedWords.Last);
            Assert.Equal(2, singlyLinkedWords.Count);
        }
Ejemplo n.º 9
0
        public void RemoveFromSimpleLinkedListEmptyDoesntThrowAnExceptionWhenValueNotFound()
        {
            var sut = new SinglyLinkedList <int>(new Node <int> {
                Value = 1
            });

            sut.Append(2);
            sut.Append(3);
            sut.RemoveByValue(4);
        }
        public void Clear_NonEmptyList_CurrentSizeIsZero()
        {
            var list = new SinglyLinkedList <int>();

            list.Append(10);
            list.Append(100);
            list.Clear();

            Assert.IsTrue(list.CurrentSize == 0);
        }
Ejemplo n.º 11
0
 public void Append_OneAndTwo_ToStringContainsAddedElements()
 {
     //Arrange
     var list = new SinglyLinkedList<string>();
     //Act
     list.Append("one");
     list.Append("two");
     //Assert
     Assert.Equal("one two", list.ToString());
 }
Ejemplo n.º 12
0
        public void TestAppend()
        {
            SinglyLinkedList <int> list = new SinglyLinkedList <int>();

            Assert.That(list.Count, Is.EqualTo(0));
            list.Append(1);
            Assert.That(list.Count, Is.EqualTo(1));
            list.Append(2);
            Assert.That(list.Count, Is.EqualTo(2));
            Assert.That(list.Last, Is.EqualTo(2));
        }
Ejemplo n.º 13
0
        public void IncludesReturnsFalseForNAData()
        {
            SinglyLinkedList list = new SinglyLinkedList();

            list.Append(3);
            list.Append(2);
            list.Append(1);
            Assert.False(list.Includes(7));
            Assert.False(list.Includes(7));
            Assert.False(list.Includes(7));
        }
Ejemplo n.º 14
0
        public void IncludesReturnsTrueForFoundData()
        {
            SinglyLinkedList list = new SinglyLinkedList();

            list.Append(3);
            list.Append(2);
            list.Append(1);
            Assert.True(list.Includes(3));
            Assert.True(list.Includes(2));
            Assert.True(list.Includes(1));
        }
Ejemplo n.º 15
0
        public void HeadRemainsFirst()
        {
            SinglyLinkedList list = new SinglyLinkedList();

            list.Append(3);
            list.Append(2);
            list.Append(1);
            Assert.Equal(3, list.Head.Data);
            list.Insert(7);
            Assert.Equal(7, list.Head.Data);
        }
Ejemplo n.º 16
0
        public void RemoveByPositionOutsideBoundariesOfSimpleLinkedListDoesntRemoveNodes()
        {
            var sut = new SinglyLinkedList <int>(new Node <int> {
                Value = 1
            });

            sut.Append(2);
            sut.Append(3);
            sut.RemoveByPosition(int.MaxValue);

            Assert.Equal(3, sut.Traverse().Count);
        }
Ejemplo n.º 17
0
        public void TestSinglyLinkedListWithAppend()
        {
            SinglyLinkedList list = new SinglyLinkedList(2);

            list.Append(3);
            list.Append(4);
            Assert.AreEqual(3, list.Length);
            Assert.AreEqual(2, list.Head.Value);
            Assert.AreEqual(4, list.Tail.Value);
            Assert.AreEqual(2, list.GetNode(1).Value);
            Assert.AreEqual(3, list.GetNode(2).Value);
        }
Ejemplo n.º 18
0
        public void InputLongerThenPattern()
        {
            var sut   = new SublistSearch();
            var input = new SinglyLinkedList <int>();

            input.Append(1);
            input.Append(2);
            var pattern = new SinglyLinkedList <int>();

            pattern.Append(1);

            Assert.True(sut.IsSubset(input, pattern));
        }
        public static void Create()
        {
            SinglyLinkedList sllist = new SinglyLinkedList();

            sllist.Append(1);
            sllist.Append(2);
            sllist.Append(3);
            sllist.Append(4);

            SinglyLinkedList.Node node = sllist.SearchNode(3);
            sllist.InsertAfter(5, node);
            sllist.InsertAtFront(6);
            sllist.Print();
        }
Ejemplo n.º 20
0
        public void RemoveByEndPositionInSimpleLinkedList()
        {
            var sut = new SinglyLinkedList <int>(new Node <int> {
                Value = 1
            });

            sut.Append(2);
            sut.Append(3);
            sut.RemoveByPosition(2);

            Assert.Equal(2, sut.Traverse().Count);
            Assert.Equal(1, sut.Traverse().First());
            Assert.Equal(2, sut.Traverse().Skip(1).First());
        }
        public void Remove_AnyElement_ReturnsTrue()
        {
            var list = new SinglyLinkedList <int>();

            list.Append(2);
            list.Append(42);
            list.Append(25);

            var removedItem = list.Remove(2);

            removedItem = list.Remove(25);
            Assert.IsTrue(removedItem == true);
            Assert.IsTrue(list.CurrentSize == 1);
        }
Ejemplo n.º 22
0
        public void RemoveNodeFromTheTailSimpleLinkedList()
        {
            var sut = new SinglyLinkedList <int>(new Node <int> {
                Value = 1
            });

            sut.Append(2);
            sut.Append(3);
            sut.RemoveByValue(3);

            Assert.Equal(2, sut.Traverse().Count);
            Assert.Equal(1, sut.Traverse().First());
            Assert.Equal(2, sut.Traverse().Skip(1).First());
        }
Ejemplo n.º 23
0
        public void ValueCollectionReturnForReadThrough()
        {
            SinglyLinkedList list = new SinglyLinkedList();

            list.Append(7);
            list.Append(7);
            list.Append(7);
            List <int> output = list.ReadThrough();

            Assert.IsType <List <int> >(output);
            foreach (var data in output)
            {
                Assert.Equal(7, data);
            }
        }
Ejemplo n.º 24
0
        public void TailIsPreservedAfterRemovalFromSimpleLinkedList()
        {
            var sut = new SinglyLinkedList <int>(new Node <int> {
                Value = 1
            });

            sut.Append(2);
            sut.Append(3);
            sut.RemoveByValue(3);
            sut.Append(4);

            Assert.Equal(3, sut.Traverse().Count);
            Assert.Equal(1, sut.Traverse().First());
            Assert.Equal(2, sut.Traverse().Skip(1).First());
            Assert.Equal(4, sut.Traverse().Skip(2).First());
        }
        private static void SinglyLinkedListImplementaion()
        {
            SinglyLinkedList <int> linkedList = new SinglyLinkedList <int>(10);

            linkedList.Append(5);
            linkedList.Append(16);
            linkedList.Prepend(1);
            linkedList.PrintList();
            linkedList.Insert(93, 2);
            linkedList.PrintList();
            linkedList.Remove(0);
            linkedList.PrintList();
            linkedList.Reverse();
            linkedList.PrintList();
            Console.ReadKey();
        }
        void IterateViaIEnumerableInterface()
        {
            var names = new SinglyLinkedList <string>();

            names.Append("hello");
            names.Append("world");

            // To obtain 'System.Collections.IEnumerator' instance;
            // call, System.Collections.IEnumerable.GetEnumerator() after casting 'SinglyLinkedList'.
            var namesEnumerator = ((IEnumerable)names).GetEnumerator();

            Assert.Equal("hello", namesEnumerator.Current);
            Assert.True(namesEnumerator.MoveNext());

            Assert.Equal("world", namesEnumerator.Current);
            Assert.False(namesEnumerator.MoveNext());
        }
Ejemplo n.º 27
0
        static void Main(string[] args)
        {
            _list.Append(Node(5));
            _list.Prepend(Node(1));
            _list.Append(Node(3));
            _list.Append(Node(4));



            Console.WriteLine(_list.ToString());

            _list.RemoveAtBeginning();
            Console.WriteLine(_list.ToString());

            _list.RemoveAtEnd();
            Console.WriteLine(_list.ToString());
        }
Ejemplo n.º 28
0
        public void AppendAddsAnElement()
        {
            var list = new SinglyLinkedList <int>();

            list.Append(10);
            list.Length.Should().Be(1);
            list.GetAtIndex(0).Should().Be(10);
        }
Ejemplo n.º 29
0
        public void TraverseRecursiveMatchesItterativeRaverse()
        {
            var sut = new SinglyLinkedList <int>(new Node <int> {
                Value = 1
            });

            sut.Append(2);
            sut.Append(3);

            var traversedSut            = sut.Traverse();
            var recursivelyTraversedSut = sut.TraverseRecursive();

            for (var i = 0; i < traversedSut.Count; i++)
            {
                Assert.Equal(traversedSut[i], recursivelyTraversedSut[i]);
            }
        }
Ejemplo n.º 30
0
        public void TestFirst()
        {
            SinglyLinkedList <int> list = new SinglyLinkedList <int>();

            list.Prepend(1);
            list.Append(2);
            list.Prepend(3);
            Assert.That(list.First, Is.EqualTo(3));
        }
Ejemplo n.º 31
0
        public void Append_value_to_end_of_single_item_list()
        {
            var list = new SinglyLinkedList<int>(new ListNode<int>(12));

            list.Append(42);

            Assert.That(list.Length, Is.EqualTo(2));
            Assert.That(list.Head.Next.Value, Is.EqualTo(42));
        }
Ejemplo n.º 32
0
        public void Append_single_node_to_empty_list()
        {
            var list = new SinglyLinkedList<int>(null);

            var node = new ListNode<int>(42);
            list.Append(node);

            Assert.That(list.Length, Is.EqualTo(1));
            Assert.That(list.Head, Is.EqualTo(node));
        }
Ejemplo n.º 33
0
        public void Append_single_node_to_end_of_multiple_item_list()
        {
            var head = new ListNode<int>(12) { Next = new ListNode<int>(77) };
            var list = new SinglyLinkedList<int>(head);

            var node = new ListNode<int>(42);
            list.Append(node);

            Assert.That(list.Length, Is.EqualTo(3));
            Assert.That(list.Head.Next.Next, Is.EqualTo(node));
        }