Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By vart

C linked list passing list as argument to the function

This is a discussion on C linked list passing list as argument to the function within the C Programming forums, part of the General Programming Boards category; I'm new with linked lists and I have bug that I can not find. I have single linked list filled ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > 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
    laserlight likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if ( found_node == my_list ) // nothing was found
    or first node contains value looked for
    Salem likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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 */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,287
    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


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