#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *h;
void push(struct node** head,int n){
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = n;
new_node->next = *head;
*head = new_node;
}
void display(struct node *head){
while(head != NULL){
printf("%d ",head->data);
head = head->next;
}
}
void swap(struct node* head){
if(head == NULL || head->next == NULL)
return;
struct node *temp = head,*temp1 = head->next,*ptr,*start = NULL;
// printf("**%d**\n",temp1->data);
while(temp != NULL&&temp1!=NULL){
temp->next = temp1->next;
temp1->next = temp;
if(start == NULL)
{
start = temp1;
//printf("**%d**\n",start->data);
}
else
ptr->next = temp1;
ptr = temp;
temp = temp->next;
if(temp!=NULL)
temp1 = temp->next;
//getchar();
}
//display(start);
h=start;
}
int main(){
struct node *start = NULL;
struct node *head;
push(&start,10);
push(&start,9);
push(&start,8);
push(&start,7);
push(&start,6);
push(&start,5);
push(&start,4);
push(&start,3);
push(&start,2);
push(&start,1);
display(start);
printf("\n");
swap(start);
start=h;
display(start);
//getchar();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0cnVjdCBub2RlewogICAgaW50IGRhdGE7CglzdHJ1Y3Qgbm9kZSAqbmV4dDsKfTsKc3RydWN0IG5vZGUgKmg7CnZvaWQgcHVzaChzdHJ1Y3Qgbm9kZSoqIGhlYWQsaW50IG4pewoJc3RydWN0IG5vZGUqIG5ld19ub2RlID0gKHN0cnVjdCBub2RlKikgbWFsbG9jKHNpemVvZihzdHJ1Y3Qgbm9kZSkpOwoJbmV3X25vZGUtPmRhdGEgPSBuOwoJbmV3X25vZGUtPm5leHQgPSAqaGVhZDsKCgkqaGVhZCA9IG5ld19ub2RlOwp9Cgp2b2lkIGRpc3BsYXkoc3RydWN0IG5vZGUgKmhlYWQpewoJd2hpbGUoaGVhZCAhPSBOVUxMKXsKCQlwcmludGYoIiVkICIsaGVhZC0+ZGF0YSk7CgkJaGVhZCA9IGhlYWQt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