示例#1
0
        public void Run()
        {
		    /* Create linked list */
		    int[] vals = {1, 3, 7, 5, 2, 9, 4};
		    var head = new LinkedListNode(vals[0], null, null);
		    var current = head;

		    for (var i = 1; i < vals.Length; i++) 
            {
			    current = new LinkedListNode(vals[i], null, current);
		    }
		    Console.WriteLine(head.PrintForward());

            var head2 = head.Clone();
            var head3 = head.Clone();
            var head4 = head.Clone();
		
		    /* Partition */
            var h = Partition(head, 5);
            var h2 = Partition2(head2, 5);
            var h3 = Partition3(head3, 5);
            var h4 = Partition4(head4, 5);
		
		    /* Print Result */
            Console.WriteLine(h.PrintForward());
            Console.WriteLine(h2.PrintForward());
            Console.WriteLine(h3.PrintForward());
            Console.WriteLine(h4.PrintForward());
        }
示例#2
0
        public void Run()
        {
            LinkedListNode first        = new LinkedListNode(0, null, null);  //AssortedMethods.randomLinkedList(1000, 0, 2);
            LinkedListNode originalList = first;
            LinkedListNode second       = first;

            for (int i = 1; i < 8; i++)
            {
                second = new LinkedListNode(i % 2, null, null);
                first.SetNext(second);
                second.SetPrevious(first);
                first = second;
            }

            LinkedListNode list1 = originalList.Clone();
            LinkedListNode list2 = originalList.Clone();
            LinkedListNode list3 = originalList.Clone();

            DeleteDupsA(list1);
            DeleteDupsB(list2);
            DeleteDupsC(list3);

            Console.WriteLine(originalList.PrintForward());
            Console.WriteLine(list1.PrintForward());
            Console.WriteLine(list1.PrintForward());
            Console.WriteLine(list1.PrintForward());

            Console.WriteLine(_tapB);
            Console.WriteLine(_tapC);
        }
示例#3
0
        public void Run(IOutput output)
        {
            const int partitioner = 5;

            // [3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 -> 9]
            var node = new LinkedListNode <int>(3,
                                                new LinkedListNode <int>(5,
                                                                         new LinkedListNode <int>(8,
                                                                                                  new LinkedListNode <int>(5,
                                                                                                                           new LinkedListNode <int>(10,
                                                                                                                                                    new LinkedListNode <int>(2,
                                                                                                                                                                             new LinkedListNode <int>(1,
                                                                                                                                                                                                      new LinkedListNode <int>(9))))))));

            var node2 = node.Clone();

            output.WriteLine(() => node);
            output.WriteNewLine();

            // Solution from the book
            {
                output.WriteLine(() => HeadTailExpansionImplementation(node, partitioner));
            }

            // Throw to the head
            {
                output.WriteLine(() => ThrowBeforeHeadImplementation((LinkedListNode <int>)node2, partitioner));
            }
        }
示例#4
0
        public LinkedListNode Clone()
        {
            LinkedListNode next2 = null;

            if (next != null)
            {
                next2 = next.Clone();
            }

            LinkedListNode head2 = new LinkedListNode(data, next2, null);

            return(head2);
        }