Thread: C linked list passing list as argument to the function

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    45

    C linked list passing list as argument to the function

    I'm new with linked lists and I have bug that I can not find. I have single linked list filled with numbers which I send as argument to the function search_tnode. If function found number that I'm searching for it returns and in main I can use new list starting with that number. If function does not find the number it returns and in main I have empty list while I want to have original list that I sent as argument. How can I achieve this?


    main:
    Code:
    my_list = search_tnode(my_list,key);
    function:
    Code:
    struct t_node * search_tnode(struct t_node *start_time, unsigned long num){
    
        struct t_node *p;
        p = start_time;
    
    
        while(p != NULL){
    
            if(p->key == num){
                return p;
            }
            p = p->next;
    
    
        }
    
        printf("Number not found %ld \n",num);
        return start_time;  }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > my_list = search_tnode(my_list,key);
    The big problem here, is that if you find something, everything before the found node is just leaked away.

    Perhaps something like
    found_node = search_tnode(my_list,key);

    Then if it's important to you
    if ( found_node == my_list ) // nothing was found
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if ( found_node == my_list ) // nothing was found
    or first node contains value looked for
    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

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    If you return NULL when an element is not found, you would have something unique to test against.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Alpo View Post
    If you return NULL when an element is not found, you would have something unique to test against.
    I personally prefer this approach, but the original design also gives a way to test the condition

    Code:
    found_node = search_tnode(my_list,key);
    
    if ( found_node->key == key )
    {
       /* element found */
    }
    else
    {
       /* original list returned, and it does not contain the key */
    }
    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

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    but this could also be:
    Code:
    found_node = search_tnode(my_list,key);
    
    if ( found_node->key != NULL )
    {
       /* element found */
    }
    else
    {
        found_node = my_list; /* not sure if this is really what OP wants to do */
    }
    Last edited by rcgldr; 08-08-2014 at 02:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a linked list to a function
    By esmeco in forum C Programming
    Replies: 6
    Last Post: 06-09-2010, 01:58 AM
  2. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  3. linked list noob - function create new list
    By dukysta in forum C Programming
    Replies: 5
    Last Post: 07-06-2007, 08:16 AM
  4. Passing Through A Variable Argument List
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 04-14-2007, 11:12 AM
  5. Variable Argument List Passing
    By Orborde in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 08:42 PM

Tags for this Thread