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.
This code has bugs..
It doesn't set the tail to NULL, if has even number of items.
It removes the last item, if it has odd number of items.
Even i dont think that this code is correct seems to be some logical mistake
Uniqueness is the key to writing blogs. So you are providing with great informative sense. I am impressed with your way of providing such good content to read.Custom Website Development WordPress
You performed a fantastic job! This is a fantastic article. This is quite appealing to me. It is quite helpful to my research. It's evident that you're passionate about the subject. I'm hoping you'll give me more information about the software. Please keep spreading the word. Custom Website Design Company