コード例 #1
0
        public static void Main()
        {
            // Create a new singly linked list of type string.
            SinglyLinkedList <string> sll = new SinglyLinkedList <string>();

            Console.WriteLine("Populate new singly linked list that is NOT a palindrome...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Farm");
            sll.Print();

            // The IsPalindrome contains 3 methods:
            // IsPalindrome: Call two helper methods: O(2n) (including helper method execution)
            // ReverseAndCopy: O(n), with O(n) space.
            // IsEqual: O(n)
            // Runtime for IsPalindrome method: O(2n) -> O(n)
            // with O(n) extra memory.
            // Same can be done using a Stack...instead of copying a new linked list.

            Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}");

            Console.WriteLine();
            Console.WriteLine("Clear linked list.");
            sll.Clear();
            Console.WriteLine($"Linked list is Empty: {sll.IsEmpty()}");
            Console.WriteLine();

            Console.WriteLine("Populate new singly linked list that IS a palindrome...");
            sll.AddFirst("Dog");
            sll.AddLast("Cat");
            sll.AddLast("Pig");
            sll.AddLast("Cat");
            sll.AddLast("Dog");
            sll.Print();

            Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}");
        }