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

FinalExam Review Sp2005: And a One and a Two

Post Answers, Questions, Comments, etc. here.

(Back to Final Exam Review Sp2005)


How many is it supposed to skip? Everyone single node...or what?
No skipping

  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 copy2;//Where we keep the second to be weaved in
    SongNode oldNext; // Need this to insert properly
    int skipped; // Number skipped currently
    
    for (int i=1; i <= count; i++)
    {
      copy = nextOne.copyNode(); // Make a copy
      copy2=newTwo.copyNode();//make a copy
      //Skip skipAmount nodes
      skipped = 1;
      while (current.next() != null)
      {
        current = current.next();
        skipped++;
      };
      
      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).insertAfter(copy2).insertAfter(copy2); // Insert the copy after this one
      current = oldNext; // Continue on with the rest
    }
  }


i don't understand this: "For count times, insertAfter one copy of one and two copies of two."

insertafter what?
Let's say the list contained 1,2,3,4 and we called 1.weaveAfter(A,B,2), we'd get 1,A,B,B,A,B,B,2,3. Mark Guzdial

I get it. sorry man.

No probs, man. :-) Mark Guzdial


If there is no skipping, what is the significance of the "skipped" in the method posted above?

It would be wrong. Mark Guzdial


is the code posted correct? how does the last copy2 eventually point to the oldNext?

current.insertAfter(copy).insertAfter(copy2).insertAfter(copy2).insertAfter(oldNext); //Would this keep all the things oldNext points to in line and following it?

Does the code above run? Yes. Does it solve the problem correctly? No. Mark Guzdial

Consider the below code. What does it really do? I think it just counts the number of nodes in the list. Mark Guzdial
      skipped = 1;
      while (current.next() != null)
      {
        current = current.next();
        skipped++;
      };


The current.insertAfter(...).insertAfter(...)... code won't even compile. insertAfter returns void – you can't cascade it. Mark Guzdial


so you loop for count number of times. then when you leave the loop, you add oldNext to the end of the linked list? (could you use the last().insertAfter(oldNext) there?)

you need a for loop that has an insert after and an incrementer like current = current.next;



How about this?

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 copy2, copy3;//Where we keep the second to be weaved in
SongNode oldNext; // Need this to insert properly
int skipped; // Number skipped currently

for (int i=1; i = count; i++)
{
copy = nextOne.copyNode(); // Make a copy
copy2 = nextTwo.copyNode();//make a copy
copy3 = nextTwo.copyNode();
//Skip skipAmount nodes
skipped = 1;
while (current.next() != null)
{
current = current.next();
skipped++;
};

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);
copy.insertAfter(copy2);
copy2.insertAfter(copy3); // Insert the copy after this one
current = oldNext; // Continue on with the rest
}
}

Wait...

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 copy2, copy3;//Where we keep the second 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
copy3 = 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);
copy.insertAfter(copy2);
copy2.insertAfter(copy3); // Insert the copy after this one
current = oldNext; // Continue on with the rest
}
}

There?

What should copy3 point to? Mark Guzdial

i dont understand how you point the last copy of B to oldNext..within the loop...but how?

copy3.setNext(oldNext); Mark Guzdial





Link to this Page