Exemple #1
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.
        }
        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)
        }