
- Forum
- Programming Talk
- C and C++
- Swapping Operation
Swapping Operation
This is a discussion on Swapping Operation within the C and C++ forums, part of the Programming Talk category; I am planning to try swapping of two nodes in the linked list using the programming language C.I want to ...
-
Swapping Operation
I am planning to try swapping of two nodes in the linked list using the programming language C.I want to know whether swapping in linked list has the same concept as Swapping in normal arrays.
-
05-01-2007, 09:34 AM #2
- Join Date
- Apr 2006
- Answers
- 124
No the concept differs for the operation of swapping of two nodes in the linked list and Swapping in normal arrays using C programming language. Swapping in normal arrays is swap of the data but in linked list it’s the pointers change and so there is just change in the index of the linked list where the node will remain.
-
You can make use of the following piece of code for swapping two nodes using linked list in C program.
node* swapsam(node *current)
{
int no; /* Number for swaping node*/
int s; /* Total number of nodes */
node *temp; /* Temporary copy of current */
node *tmp; /* Temporary variable */
s=number(current);
if(s<=1)
{
printf("\nCannot swap Single node\n");
return(current);
}
printf("\nEnter number whose node to be swapped with the next\n");
scanf("%d",&no);
temp=current;
if(current->sno==no)
{
tmp=current->next;
current->next=current->next->next;
tmp->next=current;
current=tmp;
return(current);
}
else
{
while(temp->next->next!=NULL)
{
if(temp->next->sno==no)
{
tmp=temp->next->next;
temp->next->next=temp->next->next->next;
tmp->next=temp->next;
temp->next=tmp;
break;
}
temp=temp->next;
}
return(current);
}
}
-
Sponsored Ads

Reply With Quote





