Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

FinalExam Review Spring2006: And a One and a Two

Questions? Answers? Comments? Comments on Questions or Answers?
(Back to Final Exam Review Spring2006)



  

public void weave(SongNode nextOne, SongNode nextTwo, int count) 
  {
    SongNode current = this; // Start from here
    SongNode copy; // Where we keep the one to be weaved in
    SongNode oldNext; // Need this to insert properly
    
    
    for (int i=1; i <= count; i++)
    {
      copy = nextOne.copyNode(); // Make a copy
      copy2 = nextTwo.copyNode(); // Make a copy
      copy2b = nextTwo.copyNode(); // Make a copy
      
          
      if (current.next() == null) // Did we actually get to the end early?
        break; // Leave the loop
      
      oldNext = current.next(); // Save its next
      current.insertAfter(copy); // Insert the copy after this one
      copy.insertAfter(copy2);
      copy2.insertAfter(copy2b);
      copy2b.setNext(oldNext);  
      current = copy2b
    }
    current = oldNext  
}
  


I believe that the method should be named "weaveTwo" and not "weave". Other than that I think its right.

Do you need the current=copy2b if you have the current=oldNext after it? Wouldn't that just reset current again?

My reasoning is that in case the loop ran more than once (that is, if count > 1), the nodes to be weaved in would be added after the end of the last one weaved in, and when the for loop was finished, old next would be added accodingly.

Hmmm I don't think that's quite right... cause you will have some of the old elements in between the ones woven in. That's the point of count is to skip a certain amount of existing nodes... So...
SongNode copy2 = nextTwo.copyNode();

if (current.next() == null) // Did we actually get to the end early?
        break; // Leave the loop

      oldNext = current.next(); // Save its next
      current.insertAfter(copy); // Insert the copy after this one
      current.insertAfter(copy2);//This may not work, cause it wouldn't be in the right order...
      current.insertAfter(copy2);//And I don't know if it will let you keep adding the same one in however many times...
      current = oldNext; // Continue on with the rest
      


I know that's not exactly complete code, but those were some of the changes I made...
I think it'll work, I haven't tried it yet though..

Erm.. I think count is the number of times the new nodes will be placed in

"For count times, insertAfter one copy of one and two copies of two." So.. that means.. that count number of times, You will get one copy of one and two copies of two.... I see - for the original, there was a skip amount. (How many nodes in between weaves).. For the new one there isn't a skipAmount, so does it default to after every one existing node?

So I guess that first one would be more like it then. I always get messed up with the weave cause there's so many quantities that you're counting.. ;P



Link to this Page