public String printForward()
 {
     if (next != null)
     {
         return(data + "->" + next.printForward());
     }
     else
     {
         return(data.ToString());
     }
 }
Beispiel #2
0
    {//Palindrom: Implement a function to check if a linked list is a palindrom.
        static void Main(string[] args)
        {
            //3210123
            int[]        a1              = new int[] { 3, 2, 1, 0, 1, 2, 3 };
            LinkListNode n3210123        = BuildListWithArray(a1);
            LinkListNode n3210123_clone1 = n3210123.clone();
            LinkListNode n3210123_clone2 = n3210123.clone();

            bool r1     = isPanlindrom(n3210123);
            bool r1half = isPanlindromReverseHalf(n3210123_clone1);
            bool r1r    = isPanlindromR(n3210123_clone2);

            Console.WriteLine("Results: {0}, {1}, {2} for Node {3}", r1, r1half, r1r, n3210123.printForward());

            //32100123
            int[]        a2               = new int[] { 3, 2, 1, 0, 0, 1, 2, 3 };
            LinkListNode n32100123        = BuildListWithArray(a2);
            LinkListNode n32100123_clone1 = n32100123.clone();
            LinkListNode n32100123_clone2 = n32100123.clone();

            bool r2     = isPanlindrom(n32100123);
            bool r2half = isPanlindromReverseHalf(n32100123_clone1);
            bool r2r    = isPanlindromR(n32100123_clone2);

            Console.WriteLine("Results: {0}, {1}, {2} for Node {3}", r2, r2half, r2r, n32100123.printForward());

            //4321908
            int[]        a3              = new int[] { 4, 3, 2, 1, 9, 0, 8 };
            LinkListNode n4321908        = BuildListWithArray(a3);
            LinkListNode n4321908_clone1 = n4321908.clone();
            LinkListNode n4321908_clone2 = n4321908.clone();

            bool r3     = isPanlindrom(n4321908);
            bool r3half = isPanlindromReverseHalf(n4321908_clone1);
            bool r3r    = isPanlindromR(n4321908_clone2);

            Console.WriteLine("Results: {0}, {1}, {2} for Node {3}", r3, r3half, r3r, n4321908.printForward());
        }