Example #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;
        }
Example #2
0
 public static void print_node(Node node)
 {
     Console.WriteLine("node level 1: " + node.data);
     Node runner = node;
     int levels = 2;
     while (runner.next != null)
     {
         runner = runner.next;
         Console.WriteLine("node level " + levels + ": " + runner.data);
         levels++;
     }
 }
Example #3
0
        public static string create_string_from_list(Node head)
        {
            StringBuilder number = new StringBuilder();
            number.Append(head.data.ToString());

            Node runner = head;
            while (runner.next != null)
            {
                runner = runner.next;
                number.Append(runner.data.ToString());
            }

            return number.ToString();
        }
Example #4
0
        public static Node create_list_from_number(int d)
        {
            Node node = null;
            List<char> chars = d.ToString().ToCharArray().ToList();

            foreach (char digit in chars)
            {
                if (node == null)
                    node = new Node(int.Parse(digit.ToString()));
                else
                    node.appendToTail(int.Parse(digit.ToString()));
            }

            return node;
        }
Example #5
0
        static void Main(string[] args)
        {
            //
            // Question 2.5
            //
            // You have jtwo numbers represented by a linked list, where each node contains
            // a single digit. The digits are stored in reverse order, such that the 1's digit
            // is the head of the list. Write a function that adds the two numbers and returns
            // the sum as a linked list.
            //
            // Example:
            // Input: (7 -> 1 -> 6) + (5 -> 9 -> 2). That is, 617+295.
            // Output: 2 -> 1 -> 9. That is, 912.
            // FOLLOW UP
            // Suppose the digits are stored in forward order. Repeat the above problem.

            Random rand = new Random();
            Node first = new Node(rand.Next(1, 9));
            Node second = new Node(rand.Next(1, 9));
            for (int x = 0; x <= 2; x++)
            {
                first.appendToTail(rand.Next(1, 9));
                second.appendToTail(rand.Next(1, 9));
            }

            Console.WriteLine("first: ");
            print_node(first);

            Console.WriteLine("second: ");
            print_node(second);

            string first_number = create_string_from_list(first);
            string second_number = create_string_from_list(second);

            Console.WriteLine("first number: " + first_number);
            Console.WriteLine("second number: " + second_number);

            int output_1 = int.Parse(Reverse(first_number));
            int output_2 = int.Parse(Reverse(second_number));
            int sum = output_1 + output_2;
            Console.WriteLine(output_1.ToString() + "+" + output_2.ToString() + "=" + sum.ToString());

            Node sum_list = create_list_from_number(int.Parse(Reverse(sum.ToString())));

            Console.WriteLine("LinkedList representing sum: ");
            print_node(sum_list);

            int output_3 = int.Parse(first_number.ToString());
            int output_4 = int.Parse(second_number.ToString());
            int sum_2 = output_3 + output_4;
            Console.WriteLine("... followup: ");
            Console.WriteLine(output_3.ToString() + "+" + output_4.ToString() + "=" + sum_2.ToString());

            Node sum_list_2 = create_list_from_number(sum_2);
            Console.WriteLine("LinkedList representing sum: ");
            print_node(sum_list_2);

            Console.ReadLine();
        }