# Swap two nodes of a Linked List

Discussion in 'C' started by shabbir, Aug 27, 2006.

Joined:
Jul 12, 2004
Messages:
15,351
382
Trophy Points:
83
The code below swaps two nodes in the linked list. The swap operation can be helpful in sorting a linked list and so its very important how to swap the nodes of linked list.

Swapping in normal arrays is swap of the data but in linked list its the pointers change and so there is just change in the index of the linked list where the node will remain.

Code:
```node* swap(node *current)
{
int rno;                         /* Roll number for swaping node*/
int t;                           /* Total number of nodes */
node *temp;                      /* Temporary copy of current */
node *tmp;                       /* Temporary variable */
t=number(current);
if(t<=1)
{
printf("\nYou cannot swap the only node\n");
return(current);
}
printf("\nEnter roll number whose node you want to swap with the next\n");
scanf("%d",&rno);
temp=current;
if(current->roll_no==rno)
{
tmp=current->next;
current->next=current->next->next;
tmp->next=current;
current=tmp;
return(current);
}
else
{
while(temp->next->next!=NULL)
{
if(temp->next->roll_no==rno)
{
tmp=temp->next->next;
temp->next->next=temp->next->next->next;
tmp->next=temp->next;
temp->next=tmp;
break;
}
temp=temp->next;
}
return(current);
}
}```
You need to call the function as below.
Code:
```head=swap(head); // head is the first node of the linked list
```
And last but not the least you should also know how the structure of the linked list is defined.
Code:
```struct list{
int roll_no;          /* Storing roll number of a node */
char name[N];         /* Storing name of node */
float marks;          /* Storing marks of a node */
struct list *next;    /* Storing next address */
};
/*****  Redefining struct list as node  *****/
typedef struct list node;
```

2. ### wreckerNew Member

Joined:
Mar 12, 2007
Messages:
40
0
Trophy Points:
0
Was really a nice try. Ya, what you stated is very correct that manipulating pointers is effective than changing values. But i would like to suggest you that once you have swapped toe nodes, you should try swapping a complete list. When i tries it for the first time was a very difficult job. you better use DEBUG.EXE for having faster results..... Coding techniques were good.....

Joined:
Jul 12, 2004
Messages:
15,351
382
Trophy Points:
83
If you know what you are doing its easy to extend but there will be something interesting when implementing it and if you don't get that you will not enjoy the coding.

4. ### shounakbossNew Member

Joined:
Oct 7, 2006
Messages:
2
0
Trophy Points:
0
could u plz paste the complete code of this program... plzzzzzz

5. ### shounakbossNew Member

Joined:
Oct 7, 2006
Messages:
2
0
Trophy Points:
0
Hey I have written following code but its giving me error ....can u correct that

Code:
```#include<stdio.h>
#include<conio.h>
struct list{
int t1;          /* Storing roll number of a node */
};
/*****  Redefining struct list as node  *****/
typedef struct list node;
void main()
{
getch();
}
node* swap(node *current)
{
int t;                           /* Total number of nodes */
int rno;
node *temp;                      /* Temporary copy of current */
node *tmp;
node *roll_no;               /* Temporary variable */
t=number(current);
if(t<=1)
{
printf("\nYou cannot swap the only node\n");
return(current);
}
printf("\nEnter number whose node you want to swap with the next\n");
scanf("%d",&rno);
temp=current;
if(t==rno)
{
tmp=current->next;
current->next=current->next->next;
tmp->next=current;
current=tmp;
return(current);
}
else
{
while(temp->next->next!=NULL)
{
if(temp->next->t==rno)
{
tmp=temp->next->next;
temp->next->next=temp->next->next->next;
tmp->next=temp->next;
temp->next=tmp;
break;
}
temp=temp->next;
}
return(current);
}
}
```

Last edited by a moderator: Sep 13, 2007

Joined:
Jul 12, 2004
Messages:
15,351
382
Trophy Points:
83
I would not do that as we already have the code in the articles for you to use it and debug your own code. If you help us to know where you are stuck we can definitely help you out.

Joined:
Jul 12, 2004
Messages:
15,351
382
Trophy Points:
83
I don't have anything apart from what is posted here.

Joined:
Jan 9, 2008
Messages:
356