Question: WAP to swap alternate nodes in a link list. Play with pointer, don't swap the values.

Algorithm: Only trick to note here is to reserve the third node while swapping two nodes. Rest is pretty simple and doesn't need an explanation.
Code:
  
void swap (struct list **list1)
{
struct list *cur, *tmp, *next;
cur = *list1;

if(cur && cur->next)
*list1 = cur->next;

//To make sure that we have at least 2 more elements to be swapped.
while(cur && cur->next)
{
next = cur->next;
tmp = next->next;
next->next = cur;
//We have to make 1->next as 4 in above example (figure).
if(tmp)
cur->next = tmp->next;
cur = tmp;
}
return;
}

Subscribe - To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.