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
        static void Main(string[] args)
        {
            DoublyLinkedList list = new DoublyLinkedList();

            list.AddFirst("1");
            list.AddFirst("2");
            list.AddFirst("3");
            list.AddLast("4");
            list.AddLast("5");
            list.AddFirst("6");
            list.DisplayList(); // 6 3 2 1 4 5

            Console.WriteLine();
            Console.WriteLine(list.GetAndRemoveFirst());
            list.DisplayList(); // 3 2 1 4 5

            Console.WriteLine();
            Console.WriteLine(list.GetAndRemoveLast());
            list.DisplayList(); // 3 2 1 4

            var findEl = list[2];

            Console.WriteLine();
            list.InsertAfter(findEl, "7");
            list.DisplayList(); // 3 2 1 7 4

            findEl = list[1];
            Console.WriteLine();
            list.RemoveElement(findEl);
            list.DisplayList(); // 3 1 7 4

            Console.WriteLine();
            list.AddFirst("8");
            list.AddLast("9");

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            DoublyLinkedList <int> list = new DoublyLinkedList <int>();

            list.Add(5);
            list.Add(7);
            list.RemoveFirst();
            list.AddFirst(5);
            list.RemoveLast();
            list.AddLast(8);

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
Ejemplo n.º 4
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.º 5
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)
        }