예제 #1
0
        public static void Main()
        {
            SinglyLinkedList <int> ll = new SinglyLinkedList <int>();

            Console.WriteLine("Populate new linked list using AddFirst, AddLast and Insert methods.");
            ll.AddFirst(3);
            ll.Print();

            // Create new node to insert before.
            Node <int> n = new Node <int>(7);

            // AddLast using node.
            ll.AddLast(n);
            ll.Print();

            // Insert 5 before the 7 node.
            ll.InsertBefore(n, 5);
            ll.Print();
            // AddLast by value.
            ll.AddLast(9);
            ll.Print();

            Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods.");
            ll.Remove(7);
            ll.Print();
            ll.RemoveFirst();
            ll.Print();
            ll.RemoveLast();
            ll.Print();
            ll.Clear();
            Console.WriteLine("Clear linked list.");
            Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}");
        }
예제 #2
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();
        }
예제 #3
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 that is NOT a palindrome...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Farm");
            sll.Print();

            // The IsPalindrome contains 3 methods:
            // IsPalindrome: Call two helper methods: O(2n) (including helper method execution)
            // ReverseAndCopy: O(n), with O(n) space.
            // IsEqual: O(n)
            // Runtime for IsPalindrome method: O(2n) -> O(n)
            // with O(n) extra memory.
            // Same can be done using a Stack...instead of copying a new linked list.

            Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}");

            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 that IS a palindrome...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Cat");
            sll.AddLast("Dog");
            sll.Print();

            Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}");
        }
예제 #4
0
        public static void Main()
        {
            // Declare new linked list of type string, using Generics<T>.
            // Big Oh notated below in Main.
            SinglyLinkedList <string> ll = new SinglyLinkedList <string>();

            Console.WriteLine("Populate new linked list using AddFirst, AddLast and Insert methods.");
            // O(1)
            ll.AddFirst("Three");
            ll.Print();

            // Create new node to insert before, O(1).
            Node <string> n = new Node <string>("Seven");

            // AddLast by node, O(n).
            ll.AddLast(n);
            ll.Print();

            // Insert 5 before the 7 node, O(n).
            ll.InsertBefore(n, "Five");
            ll.Print();
            // AddLast by value, O(n).
            ll.AddLast("Nine");
            ll.Print();

            Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods.");
            ll.Remove("Seven");                                         // O(n)
            ll.Print();                                                 // O(n)
            ll.RemoveFirst();                                           // O(1)
            ll.Print();
            ll.RemoveLast();                                            // O(n)
            ll.Print();
            ll.Clear();                                                 // O(1)
            Console.WriteLine("Clear linked list.");
            Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1)
        }