linked list swap function

This is a discussion on linked list swap function within the C Programming forums, part of the General Programming Boards category; how exactly do i store b without overwriting it?...

  1. #16
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    how exactly do i store b without overwriting it?

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to be doing this backwards. You don't seem to have any idea of what you want your function to be used for. You're just throwing random scenarios at some code and trying to make it fit. Don't you have anything in mind that it's supposed to do?

    You get an idea of what you want some code to do, then you make it do it. You don't just write random test cases and try to have it fit them all without having any real goal in mind.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    quzah, i tried the physical object approach (with cards labelled A,B,C) in my last post i came up with:

    Code:
    charNode *temp = a;
       charNode *temp2 = a->next;       temp2 is b
       charNode *temp3 = b->next;        temp3 is c 
       a->next = b->next; /* form A, C works until here */
    
    temp2 = temp3;
    I can't seem to store B without overwriting something

  4. #19
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    the strange thing is:

    Code:
    a->next = b->next;
    after the above line, that's when B goes missing. I don't know how to get it back after that. Any ideas?

  5. #20
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    I'm still stuck on it(all day actually). Any other hints? I've tried following quzah's original code, i understand it perfectly. I can't apply the same approach to making ABC to ACB. After thinking about how to do it with 3 cards, i then try coding it only to get an endless loop or an unxpected result :|

  6. #21
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    charNode *temp = a;
       charNode *temp2 = a->next;       temp2 is b//actually no use of  
                                                            these two temp variables
       charNode *temp3 = b->next;        temp3 is c //
       a->next = b->next; /* form A, C works until here */
    now your list becomes {A,C} OR {B,C}
    Now your aim is {A,C,B}.how would you do it??.pointing the next of c to b and next of b to Null.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  7. #22
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    are you saying my code is valid up until a->next = b->next;?
    Because when i print it out without the last line only 3 elements are printed.

    (edit) sorry i meant 2.

  8. #23
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>are you saying my code is valid up until a->next = b->next;?
    yeah.

    >>Because when i print it out without the last line only 3 elements are printed.
    You are passing three elements a,b,c.your b is lost what are the 3 elements printed.n foremost how you are printing?
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  9. #24
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok, at least i got it to print A,C,B but D is missing

  10. #25
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by Axel
    ok, at least i got it to print A,C,B but D is missing
    thats beacause you set c->next to b which was earlier d.now you lost d.so make a statement atlast which does b->next = d.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  11. #26
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i tried b->next = a->next->next->next which gives an endless loop unless i put a NULL. I think i have to set the last element in the list to NULL?

  12. #27
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    my print function is something like:
    Code:
     while (start != NULL )
       {
          printf("\n %d %c %.2f %.2f\n", element , start->cityname, start->numberone,    start->numbertwo);
    }

  13. #28
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    n/m finally got it thanks!

  14. #29
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    programming is little different from chemistry.One should make the diagram of variable on a notebook and see how pointers are moving instead of hit and trial method untill v get feeling of
    EUREKA
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  15. #30
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok it doesn't seem like what i wanted. Say if i had


    A, B, C, D, E

    start with B and C

    swap the two.

    A, C, B, D, E

    if the IF evaluates to true then everything is OK. Continue on...

    go to D and E swap it.

    A, C, B, E, D

    The if evaluates to false so i need to unswap it.

    A, C, B, D, E


    at the moment here's what happens ( i have a while loop which just goes and runs the swap function till it hits the end)

    A,B,C,D,E
    A,C,B,D,E
    A,C,D,B,E
    A,C,D,E,B

    i somehow need to UNSWAP if the if condition evaluates to false. Any hints ?
    Last edited by Axel; 10-26-2005 at 08:27 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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