Ok. The purpose of the function seems clear enough. Does it work now?
I'd recommend to separate testing of Toswap() from testing swap().
ie. don't call swap yet. Instead, just display the data held by first and second so you can see that the correct nodes have been found and saved to them.
You should call swap after the while loop exits.
Like so:
1 2 3 4 5 6 7
|
while(....)
{
// first and second are found. The while is terminated.
}
// swap(first, second);// hold off on testing this
cout << first->value << ' ' << second->value;// examine values to verify the Toswap function is finding the correct nodes.
|
Questions:
1) Why loop while current->next ? Do you want to avoid the last node for some reason?
If not, loop while( current ).
2) I think the bool values aren't needed. The pointers themselves can be tested instead.
You really don't need to be allocating new nodes!!
If you make lines 12,13 this instead:
1 2
|
Dnode *first = NULL;
Dnode *second = NULL;
|
then you can use first and second as the bool values!
1 2 3 4 5 6 7 8 9 10 11
|
Dnode *current = head;
Dnode *first = NULL;
Dnode *second = NULL;
while( current && !(first && second) )// loop will end when both pointers have been assigned
{
if(counter == pos1) first = current;
if(counter == pos2) second = current;
current = current->next;
counter++;
}
|
Lines like 20 and 21 in your code actually wreck the list! You've assigned current->next = current. None of that is needed. The swap function will make all needed changes to other pointer assignments.
See if all this helps.