private void button7_Click(object sender, EventArgs e)
        {
            //http://www.jusfortechies.com/java/core-java/deepcopy_and_shallowcopy.php

            RandomNode node1 = new RandomNode(1);
            RandomNode node2 = new RandomNode(2);
            RandomNode node3 = new RandomNode(3);
            RandomNode node4 = new RandomNode(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            //set random pointers
            node1.Random = node3;
            node2.Random = node4;
            node3.Random = node4;
            node4.Random = node1;


            //Now we created linkedlist with next and random pointer..

            RandomNode duplicate = deepCopy(node1);

            this.textBox8.Text = NodeHelper.GetStringByRandomNode(duplicate);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            //http://rajpal.x10.mx/copy-a-linked-list-with-next-and-random-pointer/


            RandomNode node1 = new RandomNode(1);
            RandomNode node2 = new RandomNode(2);
            RandomNode node3 = new RandomNode(3);
            RandomNode node4 = new RandomNode(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            //set random pointers
            node1.Random = node3;
            node2.Random = node4;
            node3.Random = node4;
            node4.Random = node1;


            //Now we created linkedlist with next and random pointer..

            RandomNode duplicate = DuplicateLinkedListWithRandomNode(node1);

            this.textBox8.Text = NodeHelper.GetStringByRandomNode(duplicate);
        }
Пример #3
0
        public static string GetStringByRandomNode(RandomNode head)
        {
            StringBuilder sb     = new StringBuilder();
            RandomNode    runner = new RandomNode();

            runner = head;
            while (runner != null)
            {
                sb.Append(runner.Data.ToString()).Append(",");
                runner = runner.Next;
            }

            return(sb.ToString());
        }
        private RandomNode deepCopy(RandomNode original)
        {
            // We use the following map to associate newly created instances
            // of RandomNode with the instances of RandomNode in the original list

            Hashtable map = new Hashtable();
            // We scan the original list and for each RandomNode x we create a new
            // RandomNode y whose data is a copy of x's data, then we store the
            // couple (x,y) in map using x as a key. Note that during this
            // scan we set y.next and y.random to null: we'll fix them in
            // the next scan
            RandomNode x = original;

            while (x != null)
            {
                RandomNode y = new RandomNode(x.Data);
                y.Next   = null;
                y.Random = null;

                map.Add(x, y);

                x = x.Next;
            }
            // Now for each RandomNode x in the original list we have a copy y
            // stored in our map. We scan again the original list and
            // we set the pointers buildings the new list
            x = original;
            while (x != null)
            {
                // we get the node y corresponding to x from the map
                RandomNode y = (RandomNode)map[x];
                // let x' = x.next; y' = map.get(x') is the new node
                // corresponding to x'; so we can set y.next = y'
                y.Next = (RandomNode)map[x.Next];
                // let x'' = x.random; y'' = map.get(x'') is the new
                // node corresponding to x''; so we can set y.random = y''
                y.Random = (RandomNode)map[x.Random];

                x = x.Next;
            }
            // finally we return the head of the new list, that is the RandomNode y
            // in the map corresponding to the RandomNode original
            return((RandomNode)map[original]);
        }
        // http://www.geeksforgeeks.org/clone-linked-list-next-random-pointer-o1-space/

        public RandomNode DuplicateLinkedListWithRandomNode(RandomNode originalhead)
        {
            RandomNode duplicatehead = null;
            RandomNode originalrunner, duplicaterunner, tempnext, tempnew;

            //Iterate through the original and create a duplicate node and place it between original and next
            //eg: Input A, B, C
            // A a B b C c

            originalrunner = originalhead;

            while (originalrunner != null)
            {
                //store the next ..be safe for overwriting
                tempnext = originalrunner.Next;

                //create a new node everytime
                tempnew        = new RandomNode(originalrunner.Data);
                tempnew.Next   = originalrunner.Next;
                tempnew.Random = null; //don't set the next

                //set the new next to the original
                originalrunner.Next = tempnew;

                //we already incremented this
                originalrunner = tempnext;
            }

            //At the end of this will loop we will have A, a, B, b, C, c

            //Now copy the arbitrary link in this fashion original->next->random = original->random->next;
            //This works because original->next is nothing but copy of original and Original->random->next is nothing but copy of random.

            originalrunner = originalhead;

            while (originalrunner != null)
            {
                //here original.next will be the copy of the orignal node.. orignal.random.next will be the copy of random
                originalrunner.Next.Random = originalrunner.Random.Next;

                //move two step to get only the orignla
                originalrunner = originalrunner.Next.Next;
            }

            //At the end of loop both orignal and copy will have same random pointer

            //restore back the orignal next property

            originalrunner = originalhead;
            //set duplicate header
            duplicatehead   = originalrunner.Next;
            duplicaterunner = duplicatehead;

            while (originalrunner != null && duplicaterunner != null)
            {
                //set the correct orignal
                originalrunner.Next = originalrunner.Next.Next;
                //this is for iteration
                originalrunner = originalrunner.Next;

                if (duplicaterunner.Next != null)
                {
                    duplicaterunner.Next = duplicaterunner.Next.Next;
                    duplicaterunner      = duplicaterunner.Next;
                }
            }

            //at the end of duplicate is copied and orignal is restored

            return(duplicatehead);
        }