Swap two nodes of a linked list

shabbir's Avatar author of Swap two nodes of a linked list
This is an article on Swap two nodes of a linked list in C.
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: C
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: C
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: C
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;
0
wrecker's Avatar, Join Date: Mar 2007
Go4Expert Member
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.....
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
shounakboss's Avatar
Newbie Member
could u plz paste the complete code of this program... plzzzzzz
0
shounakboss's Avatar
Newbie Member
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 */
    struct list *next, *head;    /* Storing next address */
};
/*****  Redefining struct list as node  *****/
typedef struct list node;
void main()
{
	node *head;
	head*= void swap(head);
	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 shabbir; 13Sep2007 at 09:13.. Reason: Code block
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by shounakboss
Hey I have written following code but its giving me error ....can u correct that
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by shounakboss
could u plz paste the complete code of this program... plzzzzzz
I don't have anything apart from what is posted here.
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Appreciated!!! But i think this is not so informatic.