Beispiel #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)}");
        }
Beispiel #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.
        }
Beispiel #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)
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Singly Linked List");
            SinglyLinkedList myList1 = new SinglyLinkedList();

            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);

            myList1.AddToFront(n1);
            myList1.AddToEnd(n2);
            myList1.AddToFront(n3);
            myList1.AddToEnd(n4);
            myList1.Print();
            myList1.RemoveNode(myList1.Head, n2.Value);
            myList1.Print();
            myList1.RemoveFirst();
            myList1.Print();
            myList1.RemoveLast();
            myList1.Print();
            myList1.RemoveLast();
            myList1.Print();

            Console.WriteLine("Doubly Linked List");
            DoublyLinkedList myList2 = new DoublyLinkedList();


            myList2.AddToFront(n1);
            myList2.AddToEnd(n2);
            myList2.AddToFront(n3);
            myList2.AddToEnd(n4);
            myList2.Print();
            myList2.RemoveNode(myList2.Head, n2.Value);
            myList2.Print();
            myList2.RemoveFirst();
            myList2.Print();
            myList2.RemoveLast();
            myList2.Print();
            myList2.RemoveLast();
            myList2.Print();
            myList2.RemoveFirst();
            myList2.Print();


            //Post Fix Calculator

            string[] expression = new string[] {
                "5", "6", "7", "*", "+", "1", "-"
            };

            Console.WriteLine("Postfix calculator");
            PostfixCalculator.Print(expression);
            Console.WriteLine("Result: " + PostfixCalculator.Calculate(expression));
        }