Thread: linked list vol.2

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    29

    linked list vol.2

    Hello again,
    I have a little problem that I hope you can help me with:
    I have linked list with its anchor.
    At the main I call a function this way:
    Code:
    void main(){
    .........
    Function (anchor);
    PrintingListFunction (anchor);
    ..........
    .........
    }
    struct* Function (struct *head){
        struct *newList;
        newList=(struct*) malloc(sizeof(struct));
        ...........
        ...........
        FreeListFunction(head);
        head=newList;
        return head;
    }
    the function must return struct* type but I call her and don't save the result anywhere (and Im not allowed to change that)
    At the function I need to do things to the list so I create a new list and Im working with it. after that i free the original list (pointed with head) and save the new list with head.
    here Is the problem-
    after I finish with the function the main gets the freed original list and not the new one.
    why? what do I miss? because I dont save the result at the main? but I change the pointer(head) to the new list.
    please help me....

  2. #2
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Because you didn't tell main() to actually use the value returned by Function().

    Try this in main():

    Code:
    anchor = Function(anchor);
    EDIT: the only other way to get it work as intended is to change to function's arguments in order to simulate call-by-reference or use global variables (ugly).

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure that you are supposed to create a completely new list, and not just modify the existing list?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    29
    I cant change the function arguments or save the result by some other pointer.
    why if I send the anchor (pointer) as an argument and at the end of the function Im pointing with it to another list it doesnt keep it outside the function?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    29
    Quote Originally Posted by matsp View Post
    Are you sure that you are supposed to create a completely new list, and not just modify the existing list?

    --
    Mats
    I didnt say I have to create new list.Its just that I thought I can do all the modification with another list and pointing it with the pointer sent as an argument (head)

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rabers View Post
    I didnt say I have to create new list.Its just that I thought I can do all the modification with another list and pointing it with the pointer sent as an argument (head)
    But since it is just a pointer to the struct, you can not change what the head/anchor value is - you can only change the contents of what head points to, not head itself. To change the value of anchor in main, it would have to be a pointer to pointer.

    So, if this is supposed to work, you should not recreate the list, but rather modify the existing one - at least, that's the best understanding I can see.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    29
    the problem is:
    its a matrix (each struct point to 2 other structs- right and down). its a sparse matrix and the function should turn it into the transposed matrix.
    thats why I created a new one and placed the original items at a "transposed way" at the new one and freed the original
    Last edited by rabers; 02-18-2009 at 04:21 PM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    29
    can I create a new list. modified it as needed. take the original list and free most of it (except the first struct wich remains the same all the time) and then link the first struct that's left to the list I created?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rabers View Post
    the problem is:
    its the a matrix (each struct point to 2 other structs- right and down). its a sparse matrix and the function should turn it into the transposed matrix.
    thats why I created a new one and placed the original items at a "transposed way" at the new one and freed the original
    Ok, so I don't know if that's hard or easy to do without creating a new matrix, but I suspect you could do it the other way around: Create a copy, then put the items back in the original matrix, in the transposed order. If I understand matrix transposition correctly, all you should need to do is replace all down-pointers with right-pointers, and all right-pointers with down-pointers, respectively.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    its a sparse matrix and the function should turn it into the transposed matrix.
    You can easily do inplace transposition of a matrix, whether it's sparse or not:

    Code:
    for(i = 0; i < ROWS; i++)
            for(j = 0; j < COLS; j++)
                    A[i][j] = A[j][i];
    Simply substitute array subscripts with the appropriate set-/get-functions, maybe something along the lines of

    Code:
                    matrix_set(A, i, j, matrix_get(A, j, i));
    If efficiency is a concern, don't use linked data structures for storing matrices.

    Greets,
    Philip

    EDIT: now that I posted the question, I can see the problem. For matrices where ROWS != COLS, this doesn't work as easily. Let me start a new post...
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Ok, so my ultra-clever approach doesn't work with MxN-matrices where M != N, so let's see...

    Aside from the fact that it's pretty stupid to return something and then not making use of it (which would solve all your problems immediately), try this:

    In Function(), create a new matrix A^T incrementally and make it the transposed of your original matrix A. Then take the first entry of A^T, i.e. A^T[0][0] in array notation, and substitute it with the corresponding node from A. Then give this node its correct value, including adjusting its pointers, and free A^T[0][0] and the remaining part of the original matrix A.

    This way, the pointer in main() points to the same address, which then is the transposed matrix.

    Greets,
    Philip
    Last edited by Snafuist; 02-18-2009 at 04:53 PM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    29
    I fixed it in an ugly way
    I modified a copy of the matrix, then i freed the rest of the original one (except the first struct -the anchor wich remains the same) and then I linked the anchor to the copy I made

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Snafuist View Post
    Ok, so my ultra-clever approach doesn't work with MxN-matrices where M != N, so let's see...

    Aside from the fact that it's pretty stupid to return something and then not making use of it (which would solve all your problems immediately), try this:

    In Function(), create a new matrix A^T incrementally and make it the transposed of your original matrix A. Then take the first entry of A^T, i.e. A^T[0][0] in array notation, and substitute it with the corresponding node from A. Then give this node its correct value, including adjusting its pointers, and free A^T[0][0] and the remaining part of the original matrix A.

    This way, the pointer in main() points to the same address, which then is the transposed matrix.

    Greets,
    Philip
    I still think that swapping all down & right links will do the job.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    29
    Quote Originally Posted by Snafuist View Post
    Ok, so my ultra-clever approach doesn't work with MxN-matrices where M != N, so let's see...

    Aside from the fact that it's pretty stupid to return something and then not making use of it (which would solve all your problems immediately), try this:

    In Function(), create a new matrix A^T incrementally and make it the transposed of your original matrix A. Then take the first entry of A^T, i.e. A^T[0][0] in array notation, and substitute it with the corresponding node from A. Then give this node its correct value, including adjusting its pointers, and free A^T[0][0] and the remaining part of the original matrix A.

    This way, the pointer in main() points to the same address, which then is the transposed matrix.

    Greets,
    Philip
    thanx. I just saw it now.
    as I said earlyer - I fixed it and it happen to be your way
    thanx again

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rabers View Post
    thanx. I just saw it now.
    as I said earlyer - I fixed it and it happen to be your way
    thanx again
    I _STILL_ think that the correct way is to swap the right/down pointers. Sorry to repeat myself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM