samedi 28 mars 2015

Unable to swap even number of elements in linked-list


Vote count:

0




I am writing code to swap elements in linked list pairwise,my code works fine with odd number of element but is not working with even number of elements

Input-1->2->3->4->5->6->7

Output-2->1->4->3->6->5->7



void sort()
{
node *ptr=head;
node *ptr1=head;
int temp;
while(ptr->next!=NULL)
{
temp=(ptr->next)->info;
(ptr->next)->info=ptr->info;
ptr->info=temp;


//This statement is not working with even number of elements as when it reaches last element it is going beyond the list and I am unable to rectify how to rectify this problem



ptr=(ptr->next)->next;
}
}


asked 57 secs ago







Unable to swap even number of elements in linked-list

Aucun commentaire:

Enregistrer un commentaire