CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    58

    Linked List swap

    Hi;

    Was working on a question. The question was to write a function that will swap a element in the list with next element in the list. I can get it to work for first element in the list but not for an element in the middle of the list.

    Any ideas?

    This is not a homework question or anything like it, jsut something i am doing myself.

    Thanks

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Linked List swap

    .....code it correctly?

    What are you looking for here?

  3. #3
    Join Date
    Mar 2009
    Posts
    58

    Re: Linked List swap

    basically looking for code where it will swap any element in the list with the next element!


    For example a link list like:

    1-2-3-4-5

    lets swap 3 with 4 so we have
    1-2-4-3-5

    But i dont want to create a new link list

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Linked List swap

    If this is a singly linked list, then you would need to change the next pointers of the second, third and fourth elements in order to efficiently swap the third and fourth elements in the list.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Linked List swap

    Show us what you tried, and we'll help you find the problem.

  6. #6
    Join Date
    Mar 2009
    Posts
    58

    Re: Linked List swap

    <code>
    struct node *swapPairs( node *l)
    {
    if(start_ptr==NULL)
    {
    Cout<<”List is NULL”<<endl;
    }
    Else
    {
    node *current = start_ptr;
    while(current->next!=l)
    current = current->next;
    node *temp = current->next; //temp = next node;
    node *prev = current; //prev = current node;
    current = temp; //current now = next node value;
    current->next=prev; //current next now = current old value
    }
    Return start_ptr;
    }

    </code>

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Linked List swap

    Good try, but you use square brackets [] for code tags here.

    Let's see....
    1) You've got a few things capitalized there which shouldn't be.
    2) What is this "l" parameter? I see you referencing start_ptr, should that be a parameter as well? Usually the name of the function and the parameter list should suggest its usage, but if I see a function called "swapPairs" that takes a single pointer, I'm going to be very confused about what I'm supposed to give it.
    3) As you observed, using this approach a special case is required to handle the first element of the list. You could get around that by defining a node** which initially points to start_ptr and then later points to the given current->next....
    4) By definition, temp == l. You don't really need two names for the same node.
    5) Here's your big problem: "current = temp". You've modified your local variable current, but you haven't modified the list at all there! If you intend to swap "current" and "temp", then you'll need to modify current->next, temp->next, *and* the node prior to current's next pointer. Try drawing a diagram to see why.
    Last edited by Lindley; May 19th, 2009 at 10:53 AM.

  8. #8
    Join Date
    Mar 2009
    Posts
    58

    Re: Linked List swap

    Thanks for that.

    I will mess around with it again tomorrow, and if any problems will post back.




    Thanks

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Linked List swap

    If this is not practice but something you need in reality then use std::list and splice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured