Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5 then the function should change it to 2->1->4->3->5, and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->5.
// I have two functions hear insertAtEnd() and insertAtBegin() for sake of fun only. you can create the linked list your own way. Just concentrate on pairWiseSwapOfGivenLL() function for the sollution.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node * insertAtEnd(struct node *head,int data)
{
if(head==NULL)
{
struct node *n1;
n1=(struct node *)malloc(sizeof(struct node));
n1->data=data;
n1->link=NULL;
head=n1;
}
else
{
struct node *n1;
n1=(struct node *)malloc(sizeof(struct node));
n1->data=data;
n1->link=NULL;
// traverse to the end of the list.
struct node *temp;
temp=head;
while((temp->link)!=NULL)
{
temp=temp->link;
}
temp->link=n1;
}
return head;
}
struct node * insertAtBegin(struct node *head,int data)
{
struct node *n1;
n1=(struct node *)malloc(sizeof(struct node));
n1->data=data;
if(head==NULL)
{
n1->link=NULL;
}
else
{
n1->link=head;
}
head=n1;
return head;
}
void printList(struct node *head)
{
if(head==NULL)
{
printf("no element in list");
}
else
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("--%d--",(temp->data));
temp=temp->link;
}
printf("\n\n");
}
}
struct node * pairWiseSwapOfGivenLL(struct node *head)
{
struct node *p1,*p2,*temp=NULL;
p1=head;
p2=p1->link;
while(p2)
{
if(p1==head)
{
p1->link=p2->link;
p2->link=p1;
head=p2;
temp=p1;
p1=p1->link;
p2=p1->link;
}
else
{
p1->link=p2->link;
p2->link=p1;
temp->link=p2;
temp=p1;
p1=p1->link;
if(p1==NULL)
{
break;
}
else
{
p2=p1->link;
}
}
}
return head;
}
int main(void)
{
struct node *head=NULL;
struct node *p,*temp=NULL;
head=insertAtEnd(head,7);
head=insertAtEnd(head,8);
head=insertAtBegin(head,6);
head=insertAtBegin(head,5);
head=insertAtEnd(head,9);
head=insertAtEnd(head,10);
//head=insertAtEnd(head,11);
//head=insertAtEnd(head,12);
printList(head);
head=pairWiseSwapOfGivenLL(head);
printList(head);
return 0;
}
// I have two functions hear insertAtEnd() and insertAtBegin() for sake of fun only. you can create the linked list your own way. Just concentrate on pairWiseSwapOfGivenLL() function for the sollution.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node * insertAtEnd(struct node *head,int data)
{
if(head==NULL)
{
struct node *n1;
n1=(struct node *)malloc(sizeof(struct node));
n1->data=data;
n1->link=NULL;
head=n1;
}
else
{
struct node *n1;
n1=(struct node *)malloc(sizeof(struct node));
n1->data=data;
n1->link=NULL;
// traverse to the end of the list.
struct node *temp;
temp=head;
while((temp->link)!=NULL)
{
temp=temp->link;
}
temp->link=n1;
}
return head;
}
struct node * insertAtBegin(struct node *head,int data)
{
struct node *n1;
n1=(struct node *)malloc(sizeof(struct node));
n1->data=data;
if(head==NULL)
{
n1->link=NULL;
}
else
{
n1->link=head;
}
head=n1;
return head;
}
void printList(struct node *head)
{
if(head==NULL)
{
printf("no element in list");
}
else
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("--%d--",(temp->data));
temp=temp->link;
}
printf("\n\n");
}
}
struct node * pairWiseSwapOfGivenLL(struct node *head)
{
struct node *p1,*p2,*temp=NULL;
p1=head;
p2=p1->link;
while(p2)
{
if(p1==head)
{
p1->link=p2->link;
p2->link=p1;
head=p2;
temp=p1;
p1=p1->link;
p2=p1->link;
}
else
{
p1->link=p2->link;
p2->link=p1;
temp->link=p2;
temp=p1;
p1=p1->link;
if(p1==NULL)
{
break;
}
else
{
p2=p1->link;
}
}
}
return head;
}
int main(void)
{
struct node *head=NULL;
struct node *p,*temp=NULL;
head=insertAtEnd(head,7);
head=insertAtEnd(head,8);
head=insertAtBegin(head,6);
head=insertAtBegin(head,5);
head=insertAtEnd(head,9);
head=insertAtEnd(head,10);
//head=insertAtEnd(head,11);
//head=insertAtEnd(head,12);
printList(head);
head=pairWiseSwapOfGivenLL(head);
printList(head);
return 0;
}
No comments:
Post a Comment