swap 2 nodes

This is a discussion on swap 2 nodes within the C Programming forums, part of the General Programming Boards category; need to swaping nodes without changing the vlue fields . my solution Code: void swap(Node *X,Node *Y) { Node *prev, ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    swap 2 nodes

    need to swaping nodes without changing the vlue fields .

    my solution

    Code:
    void swap(Node *X,Node *Y)
    {
    	Node *prev, *next, *temp;
    	temp=X;
    	prev=(X->prev);
    	next=(X->next);
    	X=Y;
    	(X->next)=next;
    	(X->prev)=prev;
    	next=(Y->next);
    	prev=(Y->prev);
    	Y=temp;
    	(Y->next)=next;
    	(Y->prev)=prev;
    }
    not working , why ?

    TNX ....

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    X=Y;


    Because that is just wrong.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    why ?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    When you are moving items in a linked list, you have to adjust the VALUES of the pointers in the list. X = Y is just internal to your function.

    Since this appears to be a double direction linked list, you'll have to set the next and prev of the one variable to the next and prev of the other while keeping those values. Then, you'll have to set the next of the prev to the Y for X and the prev of the next to the Y for X and the same for Y to X -- Keeping in mind that you'll need to preserve those values prior to changing them in the other.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    i don`t understand but thank you anyway .

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    All X=Y will do is change the name you have given the objects: the object you used to be calling Y is now also called X (and is still called Y), and the object you used to be calling X is now called --- well, fortunately for you, you kept that in temp! Therefore, your attempt later on to put Y->next and Y->prev into your placeholders is doomed, unless you change them to temp->next and temp->prev.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    NULL <-(a) <-> (b) <-> (c) <-> (d)->NULL

    In the above, if you get X=b and Y=d then you'd have to do something like this:

    prev = X.prev
    next = X.next
    X.prev = y.prev
    X.next = y.next
    y.prev.next = X
    y.next.prev = X

    Now for Y

    Y.prev = prev
    Y.next = next
    prev.next = Y
    next.prev = Y

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    Quote Originally Posted by Kennedy View Post
    NULL <-(a) <-> (b) <-> (c) <-> (d)->NULL

    In the above, if you get X=b and Y=d then you'd have to do something like this:

    prev = X.prev
    next = X.next
    X.prev = y.prev
    X.next = y.next
    y.prev.next = X
    y.next.prev = X

    Now for Y

    Y.prev = prev
    Y.next = next
    prev.next = Y
    next.prev = Y
    hmmmm.... sorry for that im bother you , but its not workin too .

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What do you mean "its not workin too"? I didn't write your program for you, just told you how to do it.

    That up there would be pseudo code.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    24
    i know..

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Do it on paper first.
    No that's no me fobbing you off, I actually seriously recommend that you work it out on paper first because it really does help!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swap nodes in linked list
    By Boaz in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 08:06 PM
  2. swapping nodes in double linked list
    By a0161 in forum C Programming
    Replies: 15
    Last Post: 10-30-2008, 06:12 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. swap nodes in doubly linked list
    By noob2c in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2003, 10:21 PM

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