Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Swap two nodes of a linked list (http://www.go4expert.com/articles/swap-nodes-linked-list-t1276/)

shabbir 27Aug2006 17:48

Swap two nodes of a linked list
 
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;


wrecker 3Apr2007 14:25

Re: Swap two nodes of a linked list
 
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.....

shabbir 3Apr2007 14:52

Re: Swap two nodes of a linked list
 
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.

shounakboss 13Sep2007 03:35

Re: Swap two nodes of a linked list
 
could u plz paste the complete code of this program... plzzzzzz

shounakboss 13Sep2007 04:46

Re: Swap two nodes of a linked list
 
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);
    }
}


shabbir 13Sep2007 09:15

Re: Swap two nodes of a linked list
 
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.

shabbir 13Sep2007 09:28

Re: Swap two nodes of a linked list
 
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.

asadullah.ansari 11Jan2008 10:53

Re: Swap two nodes of a linked list
 
Appreciated!!! But i think this is not so informatic.


All times are GMT +5.5. The time now is 06:55.