GeeksforGeeks » Linked List specific questions
swap node of double link list....debug help
(4 posts)-
could you please help me debug this code.
this is a code to swap two node of double link list.
i am not able to figure out , where i am doing wrong :( :(here is the code:-
dll* swap_node(dll *head,dll *node1,dll *node2) { dll *tmp; int flag=0; if(node1->prev!=NULL) { node1->prev->next=node2; } else { flag=1; } if(node1->next!=NULL) { node1->next->prev=node2; } if(node2->prev!=NULL) { node2->prev->next=node1; } if(node2->next!=NULL) { node2->next->prev=node1; } tmp=node1->next; node1->next=node2->next; node2->next=tmp; tmp=node1->prev; node1->prev=node2->prev; node2->prev=tmp; if(flag==1) { head=node2; } return head; }Thanks in advance;
-
There are many problems with the code. Why not simply swap the data at nodes instead of changing links?
-
no error....
-
@lalit, coz its a good practice to swap the links as a node may contain huge data.
So, it will be inefficient if do data wise swapping.
Reply
You must log in to post.