Thread: linked list search

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    linked list search

    Hello, I read an article on linked list here: C Linked List Data Structure Explained with an Example C Program

    Code:
    struct test_struct* search_in_list(int val, struct test_struct **prev)
    {
        struct test_struct *ptr = head;
        struct test_struct *tmp = NULL;
        bool found = false;
        
        printf("\n Searching the list for value [%d] \n",val);
        
        while(ptr != NULL)
        {
            if(ptr->val == val)
            {
                found = true;
                break;
            }
            else
            {
                tmp = ptr; 
                ptr = ptr->next;
            }
        }
        
        if(true == found)
        {
            if(prev)
                *prev = tmp;
            return ptr;
        }
        else
        {
            return NULL;
        }
    }
    
    
    ......
    ........
    struct test_struct *prev = NULL;
        struct test_struct *del = NULL;
        
        printf("\n Deleting value [%d] from list\n",val);
        
        del = search_in_list(val,&prev);
    What is "if(prev)"? Wouldn't "prev" always have the same value? Secondly, if tmp is NULL (which will be the case when the loop if(ptr->val == val) finds a match the first time it is run), is *prev assigned a NULL?
    Last edited by RounakJain; 05-07-2013 at 09:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list search and count
    By McElmurry in forum C Programming
    Replies: 4
    Last Post: 11-29-2010, 03:54 PM
  2. Help on Linked List Search
    By sivapc in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2009, 12:54 AM
  3. linked list search
    By rocketman03 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 06:48 AM
  4. linked list search question..
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 10-29-2008, 04:40 PM
  5. Linked list search??
    By ren in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 08:49 AM

Tags for this Thread