Esempio n. 1
0
int Q_Find( queue *q, void *data, int(*Comp)( const void *, const void * ) )
{
    void *d;
    d = Q_First( q );
    do
    {
        if ( Comp( d, data ) == 0 ) return True_;
        d = Q_Next( q );
    }
    while ( !Q_End( q ) );

    if ( Comp( d, data ) == 0 ) return True_;

    return False_;
}
Esempio n. 2
0
int Q_Sort(queue *q, int (*Comp)(const void *, const void *))
{
      int         i;
      void        *d;
      datanode    *dn;

      /* if already sorted free memory for tag array */

      if (q->sorted)
      {
            free(Q_index);
            free(posn_index);
            q->sorted = False_;
      }

      /* Now allocate memory of array, array of pointers */

      Q_index = malloc(q->size * sizeof(q->cursor->data));
      if (Q_index == NULL)
            return False_;

      posn_index = malloc(q->size * sizeof(q->cursor));
      if (posn_index == NULL)
      {
            free(Q_index);
            return False_;
      }

      /* Walk queue putting pointers into array */

      d = Q_First(q);
      for (i=0; i < q->size; i++)
      {
            Q_index[i] = d;
            posn_index[i] = q->cursor;
            d = Q_Next(q);
      }

      /* Now sort the index */

      QuickSort(Q_index, 0, q->size - 1, Comp);

      /* Rearrange the actual queue into correct order */

      dn = q->head;
      i = 0;
      while (dn != NULL)
      {
            dn->data = Q_index[i++];
            dn = dn->next;
      }

      /* Re-position to original element */

      if (d != NULL)
            Q_Find(q, d, Comp);
      else  Q_First(q);

      q->sorted = True_;

      return True_;
}