开发者

C LinkedList add Ordered

I have a linked List c file/head as an independent library i am using for a project. I dont yet have a add ordered method in the library. My problem is writing a compare function because 开发者_StackOverflowi want to compare on different items for different projects. How do i create a compare function in my main for whatever project i am using then pass and use that function into the add_ordered method in my linked list library? I cant seem to find a workable solution to passing in the function and using it within my linked list.

here is a uncompiled version of my add_ordered and compareto methods (compare to method will be different for each file):

void ll_add_ordered(ll_node *head, void *d){
 ll_node *cur;
 ll_node *temp;

 if(head->size == 0){
  ll_add_first(head, d);
 }else{
  temp = (ll_node *)malloc(sizeof(ll_node));
  temp->data = d;

  for(cur = head->next; cur->data != NULL && compareTo(temp->data, cur->data); cur = cur->next)
   ;

  temp->next = cur;
  temp->prev = cur->prev;
  cur->prev->next = temp;
  cur->prev = temp;

  head->size++;
 }
}

int compareTo(proc *first, proc *second){
 if(first->arrival < second->arrival)
  return -1;
 else if(first->arrival > second->arrival)
  return 1;
 else
  return 0;
}


One approach is to pass in a pointer to a function that takes void * arguments,e.g. int (*cmp) (void * lhs, void * rhs). In this case, it would be the responsibility of cmp to cast its arguments to the right type.

You might be able to do something a little more type safe with macros and token pasting but personally I find that to be overkill. If you are instantiating the linked list, then you know what the types are and can pass in a suitable comparison function.


This will define a type called compareFunc:

typedef int(*compareFunc)(void *first, void *second);

Now you would rewrite ll_add_ordered as:

void ll_add_ordered(ll_node *head, void *d, compareFunc compareTo) { . . .

This will allow you to pass in any function that matches your compareTo function signature into ll_add_ordered and have it called when doing the comparison.


A simple solution would be to use a function pointer, and pass the pointer to the function.

I would recommend you declare an alias for the function signature as well, lest you get crazy after a while :)

I'm assuming ll_node.data is of type void *. Thus your linked list requires two things:

  1. The comparison function takes two void pointers, compares them (using insider knowledge). The comparison function must know how to deal with the data. This requirement is of course implicitly enforced on the person/code/evil co-worker calling ll_add_ordered.
  2. The comparison function returns 1, 0 or -1.

The type alias for such a function pointer would be:

typedef int (*ll_comp_func)(void *, void*);

If you think this looks crazy, you're totally right. It serves to make you crazyhomicidial late nights when nothing works. Anyhow, what it really does is create a typedef for a function pointer and it calls the alias ll_comp_func.

You would then alter your ll_add_ordered into the following form:

void ll_add_ordered(ll_node *head, void *d, ll_comp_func comparison){
   // Do stuff.
   int order = comparison(temp->data, cur->data);
   // Do even more stuff.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜