0

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

1
  • Why is the function "wrong"? What behavior are you expecting, and what are you getting? Commented Nov 26, 2013 at 8:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

you are right, but there may be cases in which the object to be found is a data structure and the method of comparison is based only on a field of this structure
@Buzz, that's what the comparison callback is for. You shouldn't care why it returns -1, 0 or 1. Only that it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.