![]() ![]() |
| |||||||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
| 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
}
}
| 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 |
| No probs, man. :-) Mark Guzdial |
| It would be wrong. Mark Guzdial |
| 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 |
| What should copy3 point to? Mark Guzdial |
| copy3.setNext(oldNext); Mark Guzdial |