Ejemplo n.º 1
0
        public static void Main()
        {
            // Create a new doubly linked list of type int.
            DoublyLinkedList <int> dll = new DoublyLinkedList <int>();

            Console.WriteLine("Populate new doubly linked list that is NOT a palindrome...");
            dll.AddFirst(3);
            dll.AddLast(5);
            dll.AddLast(7);
            dll.AddLast(9);
            Console.WriteLine("Print:");
            dll.Print();
            Console.WriteLine("Print Reverse:");
            dll.PrintReverse();

            // The IsPalindrome contains 3 methods:
            // IsPalindrome: O(2n)
            // ReverseAndCopy: O(n), with O(n) space.
            // IsEqual: O(n)
            // Runtime for IsPalindrome method execution: O(2n) -> O(n)
            // with O(n) extra memory.
            // Still trying to work in generics, only accepts type int right now.

            Console.WriteLine($"Is linked list a palindrome: {dll.IsPalindrome(dll.Head)}");
            Console.WriteLine();
            Console.WriteLine("Clear linked list.");
            dll.Clear();
            Console.WriteLine($"Linked list is Empty: {dll.IsEmpty()}");
            Console.WriteLine();
            Console.WriteLine("Populate new doubly linked list that IS a palindrome...");
            dll.AddFirst(3);
            dll.AddLast(5);
            dll.AddLast(7);
            dll.AddLast(5);
            dll.AddLast(3);
            Console.WriteLine("Print:");
            dll.Print();
            Console.WriteLine("Print Reverse:");
            dll.PrintReverse();

            Console.WriteLine($"Is linked list a palindrome: {dll.IsPalindrome(dll.Head)}");
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            // Declare new doubly linked list of type int, using Generics<T>.
            // Big Oh notated below in Main.
            DoublyLinkedList <int> ll = new DoublyLinkedList <int>();

            Console.WriteLine("Populate new doubly linked list of type int.");

            // AddFirst: O(1)
            ll.AddFirst(3);

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

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

            // Insert 5 before the 7 node, O(n).
            ll.AddBefore(n, 5);
            // AddLast by value, O(1).
            ll.AddLast(9);
            Console.WriteLine("Print:");
            ll.Print(); // O(n)
            Console.WriteLine("Print Reverse:");
            // Print Reverse: O(n)
            ll.PrintReverse();
            Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods.");
            ll.Remove(7);                                               // Remove by value (search): O(n)
            ll.Print();
            ll.RemoveFirst();                                           // O(1)
            ll.Print();
            ll.RemoveLast();                                            // O(1)
            ll.Print();
            ll.Clear();                                                 // O(1)
            Console.WriteLine("Clear linked list.");
            Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1)

            // The Upshot: Doubly Linked Lists improve all operations at Tail from O(n) to O(1).
            // Still O(n) to search or print, methods requiring complete or partial iteration.
            // I used a Count variable, unlike the single list, because they improve the code.
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            // Declare new doubly linked list of type int, using Generics<T>.
            // Big Oh notated below in Main.
            DoublyLinkedList <int> ll = new DoublyLinkedList <int>();

            Console.WriteLine("Populate new linked list using AddFirst, AddLast and AddBefore.");
            // O(1)
            ll.AddFirst(3);

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

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

            // Insert 5 before the 7 node, O(n).
            ll.AddBefore(n, 5);
            // AddLast by value, O(1).
            ll.AddLast(9);
            Console.WriteLine("Print:");
            ll.Print(); // O(n)
            Console.WriteLine("Print Reverse:");
            // Print Reverse
            ll.PrintReverse();                                          // O(n)
            Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods.");
            ll.Remove(7);                                               // O(n)
            ll.Print();
            ll.RemoveFirst();                                           // O(1)
            ll.Print();
            ll.RemoveLast();                                            // O(1)
            ll.Print();
            ll.Clear();                                                 // O(1)
            Console.WriteLine("Clear linked list.");
            Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1)
        }