Question

following to this previous question about Void * on double linked lists I'm now wondering on how implement a find function that handles with this type of linked list. i made some steps forward with this and i'm a bit confused.. the base idea is the following

void find(list_el *el, linked_list *l_ptr )
{
    list_el *p = l_ptr->head;
    while(p != NULL)
    {
       if ((l_ptr->data_compare_func(el->data,p->data)) == 0) /* 0 means exact match*/
       {
           printf("Found\n");
       }
           p = p->next;
    } 
}

as it is now written is wrong but i can't do better than this because I do not have a clear idea of how this works. I'd like to make it dynamic and i do not have ideas on how is the better way to handle the el parameter... should i declare it as list_el or should i declare it as void* ? a problem could be that if i declare it as list_el every time i have to call find i have to create a new list_el fill it and pass it to the find method. i think it's not a good way..for the "void * solution " i don't know if it will change something perhaps it would be even worse... as you can deduce from my ramblings are very confused any help will be useful..

Thanks

Was it helpful?

Solution

Of course the find() function should not require the caller to wrap the data in a list element.

It sounds as if you're becoming more confused about this due to the list holding void * as its data, not sure why.

If you had a list of int, then I guess you would expect to have a bool find(const list_el *head, int value); function to look for a certain number. It's no different for void *, but of course the actual comparison can be a bit more tricky since it's less well-defined what equality means.

That is, of course, why your list seems to use an application-supplied callback function to do the comparison.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top