Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DoublyLinkedList <Vagon> Train = new DoublyLinkedList <Vagon>();

            Vagon vagon0 = new Vagon(0, 10, 15);
            Vagon vagon1 = new Vagon(1, 15, 15);
            Vagon vagon2 = new Vagon(2, 20, 15);
            Vagon vagon3 = new Vagon(3, 25, 15);

            GetTrain(Train);
            Train.Add(vagon0);
            GetTrain(Train);
            Train.Add(vagon1);
            GetTrain(Train);
            Train.Add(vagon2);
            GetTrain(Train);
            Train.Add(vagon3, 0);
            GetTrain(Train);
            Train.Remove(vagon1);
            GetTrain(Train);
            Train.Remove(1);
            GetTrain(Train);
            Train.Edit(0, new Vagon(4, 120, 12));
            GetTrain(Train);
        }
Exemplo 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.
        }
Exemplo 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)
        }