Exemplo n.º 1
0
        public void appendToTail(int d)
        {
            Node end = new Node(d);
            Node n = this;
            while (n.next != null)
            {
                n = n.next;
            }

            n.next = end;
        }
Exemplo n.º 2
0
 public static void print_node(Node head)
 {
     Console.WriteLine("node level 1: " + head.data);
     Node runner = head;
     int levels = 2;
     while (runner.next != null)
     {
         runner = runner.next;
         Console.WriteLine("node level " + levels + ": " + runner.data);
         levels++;
     }
 }
Exemplo n.º 3
0
        public static bool check_if_palindrome(Node head)
        {
            List<int> values = new List<int>();
            Node runner = head;
            values.Add(head.data);

            while (runner.next != null)
            {
                runner = runner.next;
                values.Add(runner.data);
            }

            List<int> reversed = new List<int>(values);
            reversed.Reverse();

            if (values.SequenceEqual(reversed))
                return true;
            else
                return false;
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            // Question 2-7
            //
            // Implement a function to check if a linked list is a palindrome.
            //

            Random rand = new Random();
            Node not_palindrome = new Node(500);
            for (int x = 0; x <= 10; x++)
            {
                not_palindrome.appendToTail(rand.Next(0, 5));
            }

            Console.WriteLine("not_palindrome: ");
            print_node(not_palindrome);
            Console.WriteLine("is palindrome?: " + check_if_palindrome(not_palindrome));

            Node palindrome = new Node(1);
            palindrome.appendToTail(0);
            palindrome.appendToTail(1);
            palindrome.appendToTail(0);
            palindrome.appendToTail(1);

            Console.WriteLine("palindrome: ");
            print_node(palindrome);
            Console.WriteLine("is palindrome?: " + check_if_palindrome(palindrome));

            Console.ReadLine();
        }