コード例 #1
0
        public static void Main()
        {
            // Create a new singly linked list of type string.
            SinglyLinkedList <string> sll = new SinglyLinkedList <string>();

            Console.WriteLine("Populate new singly linked list of strings with duplicates...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Cat");
            sll.AddLast("Dog");
            sll.Print();

            // Remove Duplicates with no memory runs in O(n^2) time, with O(1) additional space.
            Console.WriteLine();
            Console.WriteLine("Remove Dupes using no additional memory:");
            sll.RemoveDupesNoMemory(sll.Head);
            sll.Print();

            Console.WriteLine();
            Console.WriteLine("Clear linked list.");
            sll.Clear();
            Console.WriteLine($"Linked list is Empty: {sll.IsEmpty()}");
            Console.WriteLine();


            Console.WriteLine("Populate new singly linked list of strings with duplicates...");
            sll.AddFirst("Jack");
            sll.AddLast("Jill");
            sll.AddLast("John");
            sll.AddLast("Jack");
            sll.AddLast("Jill");
            sll.Print();

            // Remove dupes using HashSet runs in O(n) time, with O(n) added memory.
            Console.WriteLine();
            Console.WriteLine("Remove Dupes using additional memory (HashSet):");
            sll.RemoveDupesWithMemory(sll.Head);
            sll.Print();
        }