static void Main(string[] args)
        {
            var sl = new SinglyLinkedList <Person>();

            var p1 = new Person("Cedrick", "Cederiosa", new DateTime(year: 2000, month: 9, day: 02));
            var p2 = new Person("Noel", "Maculbe", new DateTime(year: 1999, month: 7, day: 11));
            var p3 = new Person("Brenn", "Hong", new DateTime(year: 1998, month: 12, day: 22));
            var p4 = new Person("Johan", "Fernandez", new DateTime(year: 1999, month: 8, day: 16));

            sl.AddToTail(p1);
            sl.AddToTail(p2);
            sl.AddToTail(p3);
            sl.AddToTail(p4);

            foreach (var person in sl)
            {
                Console.WriteLine(person);
            }

            Console.WriteLine("Using SearchForPosition using the default comparer");
            var resultingPosition = sl.SearchForPosition(p4);

            Console.WriteLine($"Found at position {resultingPosition}");

            Console.WriteLine("Using SearchForPosition using PersonNameLenghtComparer");
            var comparer = new PersonNameLengthComparer();

            resultingPosition = sl.SearchForPosition(p2, comparer);
            Console.WriteLine($"found at position {resultingPosition}");

            Console.ReadLine();
        }
Beispiel #2
0
        public void ElementsCanBeRemovedFromLinkedList()
        {
            for (int i = 0; i < 10; i++)
            {
                var linkedList     = new SinglyLinkedList <string>();
                var expectedString = new StringBuilder();

                for (int j = 0; j < 10; j++)
                {
                    linkedList.AddToTail(j.ToString());
                    if (j != i)
                    {
                        expectedString.Append(j.ToString());
                    }
                }
                linkedList.RemoveItem(i.ToString());

                var actualString = new StringBuilder();
                for (var node = linkedList.GetHeadNode(); node != null; node = node.GetNextNode())
                {
                    actualString.Append(node.Data);
                }

                Assert.AreEqual(expectedString.ToString(), actualString.ToString());
            }
        }
Beispiel #3
0
        public void LinkedListCanStoreChainOfElements()
        {
            var linkedList     = new SinglyLinkedList <string>();
            var expectedString = new StringBuilder();

            for (int i = 1; i < 10; i++)
            {
                linkedList.AddToTail(i.ToString());
                expectedString.Append(i.ToString());
            }

            var actualString = new StringBuilder();

            for (var node = linkedList.GetHeadNode(); node != null; node = node.GetNextNode())
            {
                actualString.Append(node.Data);
            }

            Assert.AreEqual(expectedString.ToString(), actualString.ToString());
        }