Thread: Linked list help

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by quzah View Post
    You didn't include how you call it, which is kinda relevant.
    Code:
    In main:
    
    type *mylist = NULL;
    ...
    yourfun( file1, file2, &mylist );

    Quzah.
    Ooops...sorry. I must have accidentally deleted it while condensing the code.

    I originally had:

    Code:
    bldList (spListData, spModList, &ptLnkList);
    but

    Code:
    bldList (spListData, spModList, ptPtLnkList);
    segfaults too.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, skip all the file crap for the moment:
    Code:
    type *p = NULL;
    ...
    foo( &p );
    ...
    void foo( type **p )
    {
        if( p )
        {
            type *n = malloc( sizeof *n );
            ...
            *p = n;
        }
    }
    That'll put the new n's address in p back in main.


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

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by quzah View Post
    Ok, skip all the file crap for the moment:
    Code:
    type *p = NULL;
    ...
    foo( &p );
    ...
    void foo( type **p )
    {
        if( p )
        {
            type *n = malloc( sizeof *n );
            ...
            *p = n;
        }
    }
    That'll put the new n's address in p back in main.


    Quzah.
    Thank you. That's seems to have worked in so far as the program no longer segfaults when it's in that function. It now segfaults in another function.

    Nearest I can figure, is that there is something wrong with how I am coding the list traversal:
    Code:
    bool srchList (struct node** ptPtLnkList, int target)
    {
        bool found;
        struct node* ptCur;
    
        found = false;
        ptCur = *ptPtLnkList;
    
        if (*ptPtLnkList == NULL)
           {
                printf("Error: list is empty");
                exit(201);
           }
    
        while (found == false && ptCur -> listEl != target && ptCur != NULL)
            {
                printf("Target not found yet 1\n");
                ptCur = ptCur -> link;
                printf("Target not found yet 2\n");
            }
    
        if (ptCur -> listEl == target)
           {
                  printf("Assigning true to found");
                  found = true;
           }
    
        return found;
    }
    I realize that no-one here is at my beck-and-call, but I would appreciate an explanation or, at least, a link to an explanation of what I am doing wrong. I really am here to learn what I am doing wrong so I can avoid it in the future.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If all you are doing is searching a list, and you don't need a pointer to a pointer. If you aren't changing the list, just pass the head of the list to it.
    Code:
    type seekit( type *list, type tofind )
    {
        type *node = NULL;
        if( list )
        {
            for( node = list; node; node = node->next )
            {
                if( node->data == tofind )
                {
                    ...yay...
                }
            }
        }
        return bad;
    }
    Pretty much just that. Heck, you don't even need to wast time with a variable 'node', you can just use the 'list' ptr. If you DO want to pass a ptrtoptr:
    Code:
    type *node = *list;
    That instead. Then just use node as per normal.


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

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