Thread: another list question....

  1. #16
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    A is spliced out.but ,U still have "loc" pointing to A
    i think it makes sense. So you are saying after this line:

    curr->next = (curr->next)->next


    A and B become split (independant of each other) and then then you just insert B after a

    like this:

    loc->next (A) = curr(B);

  2. #17
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    ah! better...
    In the middle of difficulty, lies opportunity

  3. #18
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i have another bit of the code which i dont understand. It handles the case where there are 3 nodes. If i have a list containing ACB and it pass it 'C' it swaps B and C around to form ABC.
    Code:
     while(temp->next->next != NULL)
            {
                if(temp->next->value=='C') // check to see if we have the correct value to swap
                {
                    loc = temp->next->next; // save B
                    temp->next->next = temp->next->next->next; // we then splice B to form B -> NULL and AC -> NULL
                    loc->next = temp->next; // assign C after B to form BC
                    temp->next = loc; // assign B after A to form AB
                    break;
                }
                temp = temp->next; 
            }
        }

    i'm not sure how it all comes together.

  4. #19
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    we start off with

    Code:
    loc = temp->next->next;

    A | C | B
    loc = b;


    Code:
     temp->next->next = temp->next->next->next;
    A | C | NULL


    Code:
    loc->next = temp->next;

    A | C | NULL | C




    Code:
     temp->next = loc;


    A | B | NULL | C

    i just don't get why i have NULL as the final result

  5. #20
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

  6. #21
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    THe point that u r missing here is , NULL is not a node. U cant have

    NULL --> C . It simply makes no sense. NULL is the end of ur list, there is nothing beyond that point.

    Let me explain thru what u have written here itself :

    we start off with



    loc = temp->next->next;



    A | C | B
    loc = b;




    temp->next->next = temp->next->next->next;


    A | C | NULL


    " all's fine upto this point "

    and loc is pointing to B ..
    temp is pointing to A.



    loc->next = temp->next;

    answer these questions :

    what is temp->next?
    what is loc->next?

    u r equating loc->next to what is stored in temp-> next, that is C right?

    U r having both temp->next and loc->next poiting to the same location now..

    temp-- > C -- > NULL
    ^
    loc -------|


    NOTE : I am not able to get it to appear correctly here.. loc - > next points to C



    temp->next = loc;

    this makes " loc " as the next node for "temp" . The previous value stored is now lost..
    the next node for "loc" is already set to be C right??

    so, we now have :

    temp --> loc -- >C -- > NULL

    or

    A -- > B -- > C --> NULL


    I hope this does the job..
    Last edited by kris.c; 10-28-2006 at 06:04 AM.
    In the middle of difficulty, lies opportunity

  7. #22
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by kris.c

    u r equating loc->next to what is stored in temp-> next, that is C right?
    loc is NULL because of the previous lline yes?

    temp->next->next = temp->next->next->next;

    as with loc->next it is also null.

    so i read the following line
    Code:
     loc->next = temp->next;
    as: make loc->next ( NULL ) point to temp->next (C)

    what is temp->next?

    C

    what is loc->next?

    NULL
    Last edited by Axel; 10-28-2006 at 06:30 AM.

  8. #23
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    >Loc is NULL because of the previous lline yes?

    tell me , how can "loc" be NULL here?

    loc -> val = B
    loc -> next = NULL
    and (temp->next)->next = NULL

    after B has been spliced out.
    In the middle of difficulty, lies opportunity

  9. #24
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    sorry i meant loc->next

    so when you do:

    Code:
     loc->next = temp->next;
    it's saying:

    NULL = C;

  10. #25
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    well, this is C and not math.

    when u do,
    loc->next = temp->next;

    U r storing the value in temp->next in loc->next... and not any other way that can be imagined.

    so, loc->next is NOT null, but the value that was stored in temp->next .. that is C..

    I can only suggest 2 things ..

    1) re-read my explanation once again slowly .. write down at each step.
    if that still does not help, parhaps U have been reading the wrong material
    2) look up for stuff on the net which display waht is happening pictorially..
    In the middle of difficulty, lies opportunity

  11. #26
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    but after loc->next = temp->next;
    is run do they *BOTH* contain C? or just loc->next ?

  12. #27
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yes, both WILL point to C

    after this , u r doing
    temp->next = loc ;

    this stores the address of loc into temp->next
    so, temp->next is pointing to loc... fine?

    which means, u have updated the value in temp->next to loc. temp->next does not point to C anymore.
    but u havent altered loc->next .
    so,loc -> next is still pointing to C

    so, the new list is :
    temp--> loc -- > C -- > NULL
    In the middle of difficulty, lies opportunity

  13. #28
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ACB

    loc=temp->next->next; /* store B somewhere */

    temp->next->next=temp->next->next->next; /* A-C-NULL */


    loc->next=temp->next; /* ACC */

    temp->next=loc; /* A->B->C->NULL */


    right ?

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do not use something like that
    Code:
    temp->next->next->next
    in a real program - you cannot suppose without checks that for example temp->next->next is not null. Check it first.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    its getting a little clearer now... except for this :
    >loc->next=temp->next; /* ACC */

    U have this instead :

    A- C- NULL
    |
    B---|

    as I said earlier both A->next and B->next point to C

    next u change A->next to B.so , u get

    A C - -> NULL
    | |
    | |
    -- B

    U better get this right now...
    Last edited by kris.c; 10-29-2006 at 03:56 AM.
    In the middle of difficulty, lies opportunity

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  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. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM