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

    Swapping 2 nodes in doubly linked list

    Hi,

    How to swapp 2 nodes in doubly linked list?? Is that means just swapping the values (becaus enode is just anode), or we have to swap also pointers??

    In case of swapping pointers, how can we do that??

    Thank you
    wael

  2. #2
    Join Date
    Jun 2009
    Posts
    89

    Re: Swapping 2 nodes in doubly linked list

    By the way ,

    There are two main cases:
    1) The two nodes to swap are next to each other,
    2) The two nodes to swap are not next to each other

    can we solve them in one solution or every situation should be solved alone??

    Thank you

  3. #3
    Join Date
    May 2009
    Posts
    23

    Smile Re: Swapping 2 nodes in doubly linked list

    Quote Originally Posted by wael.salman View Post
    Hi,

    How to swapp 2 nodes in doubly linked list?? Is that means just swapping the values (becaus enode is just anode), or we have to swap also pointers??

    In case of swapping pointers, how can we do that??

    Thank you
    wael
    I think it depends on how you want to implemented. But usually we just swapping the pointer without regard of the value of type of value and it is easier to implemented.
    When you want to delete or swap in link-list style, I usually check only when it relates to root, middle and at end node.

  4. #4
    Join Date
    Jun 2009
    Posts
    19

    Re: Swapping 2 nodes in doubly linked list

    The quick way would be just to remove both nodes, retrieving their pointers, and then insert them back in, the other way.

  5. #5
    Join Date
    Jun 2009
    Posts
    89

    Re: Swapping 2 nodes in doubly linked list

    I guess it is just swapping the node values and that is all. We do not have to swap pointers.

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

    Re: Swapping 2 nodes in doubly linked list

    Quote Originally Posted by wael.salman
    I guess it is just swapping the node values and that is all. We do not have to swap pointers.
    Although that will work, if the node values are expensive to copy, then it would be cheaper to relink pointers as prime1999 described.
    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

  7. #7
    Join Date
    Feb 2010
    Posts
    1

    Re: Swapping 2 nodes in doubly linked list

    Quote Originally Posted by wael.salman View Post
    By the way ,

    There are two main cases:
    1) The two nodes to swap are next to each other,
    2) The two nodes to swap are not next to each other

    can we solve them in one solution or every situation should be solved alone??

    Thank you
    we have to take two steps in common and two steps in specific for each case.


    common steps:
    1.right pointer of the first node to point where the right pointer of the second node currently pointing.
    2.left pointer of the second node to point where the left pointer of the first node is currently pointing.

    if the nodes r next to each other then
    1.make left pointer of the first node to point the second node.
    2.make right pointer of the second node to point the first node.

    if the nodes r not next to each other then
    1.make left pointer of the first node to point where the left pointer of the second node pointing currently.
    2.make right pointer of the second node to point where the right pointer of the first node currently pointing.

Tags for this Thread

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