Exforsys
+ Reply to Thread
Results 1 to 3 of 3

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 ...

  1. #1
    rachelle is offline Member Array
    Join Date
    Apr 2006
    Answers
    97

    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.


  2. #2
    Rahulbatra is offline Senior Member Array
    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.


  3. #3
    cyrus is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    128
    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("&#37;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



Latest Article

Network Security Risk Assessment and Measurement

Read More...