how to swap in doubly linked list

Hi guys, how ya doing. Here I have a small problem with my doubly linked list. The thing is that I am trying to do in the pasted code below is that, I am trying to swap the two nodes by it's position. I tried it but smells like it doesn't work at all. Can you guys suggest me what's wrong or better way to do it. please guys i need your help.
I will really appreciate.
thanks

/* swap the data between two nodes */
void ALinkedList::swap(int pos1, int pos2) {
Node *curr1 = head;
Node *curr2 = head;
int temp;
int temp1;
int temp2;

for(int i=0;i<pos1;i++)
curr1 = curr1 ->next;
temp1 = curr1->data;

for(int j=0;j<pos2;j++)
curr2 = curr2 ->next;
temp2 = curr2->data;

temp = temp1;
temp1 = temp2;
temp2 = temp;

return;
}
[code] "Code tags, please" [/code]
How do you expect that data change, if you aren't changing data?

Edit: instead of swapping the values you could adjusts the links
Last edited on
Topic archived. No new replies allowed.