GeeksforGeeks » Linked List specific questions

swap node of double link list....debug help

(4 posts)
  • Started 5 months ago by atul007
  • Latest reply from Guddu Sharma

Tags:

  1. atul007
    Member
    Posted 5 months ago #

    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;

  2. lalit
    guest
    Posted 5 months ago #

    There are many problems with the code. Why not simply swap the data at nodes instead of changing links?

  3. mukesh kumar dhaker
    guest
    Posted 5 months ago #

    no error....

  4. Guddu Sharma
    Member
    Posted 1 month ago #

    @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.

RSS feed for this topic