Musings
            Public · Protected · Private
        
        
    Copy a LinkedList with a random pointer to one of the list element
    
    - 
                    2008-07-15 12:57Problem statement: I have alinked list of objects. Each object has a pointer called Randomobj that points to any of its elemen. So randomobj can point to self or one of any previous / null / one of any later....
 - 
                    2008-07-15 13:01// Here is asimple LinkedList class class LlEleWithRandom { public LlEleWithRandom Randomobj = null; public LlEleWithRandom next = null; public int value; public LlEleWithRandom(int v) { value = v; } } // Simple populate some test data... LlEleWithRandom GivenSourceRoot = new LlEleWithRandom(0); LlEleWithRandom temp = GivenSourceRoot; ArrayList al = new ArrayList(); al.Add(GivenSourceRoot); for (int i = 1; i < 11; i++) { temp.next = new LlEleWithRandom(i); temp = temp.next; al.Add(temp); } Random r = new Random(DateTime.Now.Second); for (int i = 0; i < al.Count; i++) { int aa = r.Next(10); ((LlEleWithRandom)al[i]).Randomobj = (LlEleWithRandom)al[aa]; } // Display the stuff label1.Text = ""; for (int i = 0; i < al.Count; i++) { label1.Text += ((LlEleWithRandom)al[i]).value.ToString() + " --" + ((LlEleWithRandom)al[i]).Randomobj.value.ToString() + "\n\r"; }
 - 
                    2008-07-15 13:41//call the real function LlEleWithRandom z = copyofmyLL(GivenSourceRoot); // Display to verify the source and destinations LlEleWithRandom tempp = z; //Display the answer Linkedlist label2.Text = ""; while (tempp != null) { label2.Text += tempp.value.ToString() + " --" + tempp.Randomobj.value.ToString() + "\n\r"; tempp = tempp.next; } label3.Text = ""; tempp = GivenSourceRoot; // Display the source LinkedList while (tempp != null) { label3.Text += tempp.value.ToString() + " --" + tempp.Randomobj.value.ToString() + "\n\r"; tempp = tempp.next; }
 - 
                    2008-07-15 13:42// Function to get the final answer private LlEleWithRandom copyofmyLL(LlEleWithRandom source) { LlEleWithRandom temp = source; LlEleWithRandom myanswer = null; LlEleWithRandom tempanswer = null; // Initially create the new elements int counter = 0; while (temp !=null) { tempanswer = new LlEleWithRandom(temp.value); tempanswer.Randomobj = temp.Randomobj; temp.Randomobj = tempanswer; temp = temp.next; if (counter == 0) myanswer = tempanswer; counter++; } temp = source; // Fill the next elements of the Answer while (temp != null) { tempanswer = temp.Randomobj; tempanswer.next = (temp.next == null) ? null : temp.next.Randomobj; temp = temp.next; } // Fill the Random and revert the source temp = source; while (temp != null) { tempanswer = temp.Randomobj; LlEleWithRandom quicktemp = tempanswer.Randomobj; temp.Randomobj = quicktemp; temp = temp.next; } return myanswer; }
 - 
                    2008-07-15 15:24Variable naming looks a little complex here.
 - 
                    2008-07-15 15:27variables के नाम अच्चे से रखो
 - 
                    2008-07-15 15:28Good code,I am thinking of using this code for writing some test cases, it will be a great idea to add some test cases here.
 - 
                    2008-07-15 15:32Ashish ji, पैसे मिलेंगे तो ठीक् code लिखेंगे | सब मुफ्त मे मिलेगा?
 - 
                    2008-07-15 15:48Question: copyofmyLL(LlEleWithRandom source) makes 3 scans along the length -- I think it can be made in two scans?
 - 
                    2008-07-15 15:52Yes I think it can be done in two passes Let me try. Also I am curious to test this with some randomobjs as null. Thanks anyway...
 
This blog is frozen. No new comments or edits allowed.