Swap two nodes in linked list

This is a discussion on Swap two nodes in linked list within the C Programming forums, part of the General Programming Boards category; I want to swap two nodes of a linked list. Like if its 12 23 34 45 then i want ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Swap two nodes in linked list

    I want to swap two nodes of a linked list. Like if its 12 23 34 45 then i want result like 21 32 43 54..... if input is 12345 then output should be 21435. something like that...


    I have written following .... but its not producing the desired output ..... somebody plz help me out

    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>


    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;
    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("&#37;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);
    }
    }

    int main()
    {
    head=swap(head); // head is the first node of the linked list
    getch();
    }
    Last edited by shounakboss; 09-12-2007 at 11:17 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    so you want to reverse the integers? give a search here or elsewhere as im sure its been covered before. if you dont want to search and want specific help, then please post an attempt at reversing a number. once you have done that im sure your help will come.

    edit: after rereading, it looks like your first example just involved reversing each integer in a list. the second example is a different case in which one number is entered and the digits are switched. so it looks like two problems.

    the first one i talked about above, the second one, a starting point would be to determine if the length of the integer is odd or even, as since 12345 has an odd number of numbers, only the first 4 are switched, however if it were 1234 it should switch to 2143 in which case all the numbers are switched. i would grab some paper, draw out 12345 and think of the steps to get it to become 21435, writing out every single step (as you know a computer would have to be told). once you have that, do it again for a number 1234, as this is the second case.

    this doesnt really have anything to do with a linked list, so im sure im misunderstanding.
    Last edited by nadroj; 09-12-2007 at 11:15 AM.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    And investigate the [code] tags!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes code tags would be wonderful. please see the edit of my previous post shounakboss.
    Quote Originally Posted by sounakboss
    I want to swap two nodes of a linked list. Like if its 12 23 34 45 then i want result like 21 32 43 54
    again this doesnt seem to involve swaping any nodes. it looks like traversing the linked list, and for each node the integer value of the data is reversed, dependent of any other node.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21