Are you asking for some kind of reusable iteration function?
If you have a generic linked list, and you would like to deal with specific
types, you could do something like this:
typedef _Bool bool; /* The type doesn't really matter as much as the
concept. */
void list_iterate(struct list_container *list,
bool (*iter)(struct list_element *,void *),
void *ptr) {
struct list_element *elem;
for(elem = list->head; elem; elem = elem->next) {
if(!iter(elem, ptr))
break;
}
}
So how might you use that?
struct search_data {
int value; /* the search value */
int result; /* the total number of items with value (for example) */
};
bool find_something(struct list_element *elem, void *ptr) {
struct search_data *search = ptr;
struct specific_elem_type *spec = elem->data;
if(spec->value == search->value) {
search->result += 1;
}
/* continue iterating the list */
return true;
}
...
{
struct search_data search;
search.value = 42;
search.result = 0;
list_iterate(list, find_something, &search);
printf("search.result is %d\n", search.result);
}
So that would basically find all instances in the linked list with a value
of 42, and update the result to store the total.
This method also allows you to change the data structure used for an
implementation, without having to change the loops that previously iterated
the data structure. This is useful if for instance you wanted to switch
from a linked list, to a splay tree, or a hash table at some point, due to
other factors in the program.
-George- Hide quoted text -
- Show quoted text -
my_list_t *
my_list_new(int flags, my_cmpfunc_t cmpfunc)
{
my_list_t *newlist;
#ifdef DS_DEBUG
printf("in my_list_new(%d, 0x%lx)\n", flags, cmpfunc);
#endif
if (flags != LIST_USERFUNC
&& flags != LIST_STACK
&& flags != LIST_QUEUE)
{
errno = EINVAL;
return NULL;
}
newlist = (my_list_t *)calloc(1, sizeof(my_list_t));
if (cmpfunc != NULL)
newlist->cmpfunc = cmpfunc;
else
newlist->cmpfunc = (my_cmpfunc_t)strcmp;
newlist->flags = flags;
return newlist;
}
I have added my objects in list.. now i want to search object_name at
specific node in list..
int
my_list_add(my_list_t *l, void *data)
{
my_listptr_t n, m;
n = (my_listptr_t)malloc(sizeof(struct my_node));
if (n == NULL)
return -1;
n->data = data;
l->nents++;
/* if the list is empty */
if (l->first == NULL)
{
l->last = l->first = n;
n->next = n->prev = NULL;
return 0;
}
if (l->flags == LIST_STACK)
{
n->prev = NULL;
n->next = l->first;
if (l->first != NULL)
l->first->prev = n;
l->first = n;
return 0;
}
if (l->flags == LIST_QUEUE)
{
n->prev = l->last;
n->next = NULL;
if (l->last != NULL)
l->last->next = n;
l->last = n;
return 0;
}
return 0;
}
I have search function like this..
int
my_list_search(my_list_t *l,
my_listptr_t *n, void *data,
my_matchfunc_t matchfunc)
{
if (matchfunc == NULL)
matchfunc = (my_matchfunc_t)my_str_match;
if (*n == NULL)
*n = l->first;
else
*n = (*n)->next;
for (; *n != NULL; *n = (*n)->next)
{
if ((*(matchfunc))(data, (*n)->data) != 0)
return 1;
}
return 0;
}
I have list search function..
int
my_list_search(my_list_t *l,
my_listptr_t *n, void *data,
my_matchfunc_t matchfunc)
{
if (matchfunc == NULL)
matchfunc = (my_matchfunc_t)my_str_match;
if (*n == NULL)
*n = l->first;
else
*n = (*n)->next;
for (; *n != NULL; *n = (*n)->next)
{
if ((*(matchfunc))(data, (*n)->data) != 0)
return 1;
}
return 0;
}
if you see the matchfunc(), its argument is a string from void *data ,
string from object_list struct(..->object_name)
So i cant re-write all the list functions for this..
please just help me.. during search, i have to get object_list data (-
object_name) from each node & just return the node if
match occurs.. if ((*(matchfunc))(data, (*n)->data) != 0)
my main() is looks like..
main()
{
my_list_t *dir_list = NULL;
my_listptr_t *obj;
object_list *objects;
dir_list = my_list_new(LIST_STACK, NULL);
while(do some operation)
{
//do something
objects->object_id = some_id;
temp_objects->object_name = some_name;
my_list_add(dir_list, (object_list *)objects);
......
......
if(my_list_search(dir_list, NULL, some_name, NULL))
do_something;
}
} //main ends
Thanks for your help!
-Ram