Front end function for qsorting linked lists.. written on my way out the
door, so I wouldn't use it on something that might screw up too bad:

/* merc.h */
#define NEXTPTR(list) ((char *)&(list->next) - (char *)list)
void *list_sort(void *list, int nextpos, int(*compar)(const void *,
  const void *));

/* some .c file */
void *list_sort(void *list, int nextpos, int(*compar)(const void *,
  const void *))
{
  int count = 0, i = 0;
  unsigned char *ptr = list;
  unsigned char **array;
  for (; ptr != NULL; count++)
    ptr = (void *)*(long *)(ptr+nextpos);
  array = malloc(sizeof(void *)*count);
  ptr = list;
  for (; ptr != NULL; ptr = (void *)*(long *)(ptr+nextpos))
    array[i++] = ptr;
  qsort(array, count, sizeof(unsigned char *), compar);
  list = NULL;
  for (count--;count >= 0; count--)
  {
    (void *)*(long *)(array[count]+nextpos) = list;
    list = array[count];
  }
  free(array);
  return list;
}

/* to use it */
list = list_sort(list, NEXTPTR(list), comparison_function);

--Palrich.

On Wed, 2004-01-28 at 15:43, Chad Simmons wrote:
> --- Valnir <[EMAIL PROTECTED]> wrote:
> > if you look at his original call to Qsort he IS passing it a count.
> > 
> > he is passing it "i" which is counted during a for loop prior to the Qsort
> > call.
> > 
> 
> 
> Yes, he's passing a count. What he is NOT passing is an array. Qsort doesn't
> operate on linked lists. If it did you wouldn't need to pass a count, cause it
> would just stop when it got to the NULL record on the end. The point of
> contention has been that (as per the man page) qsort takes the address to the
> first element in an array, the count of elements in the array, and a pointer 
> to
> a comparison function. 
> 
> ~Kender



Reply via email to