Re: Using a link list over an array.
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Wed, 02 Jul 2008 01:25:59 +0100
pete <pfiland@xxxxxxxxxxxxxx> writes:
Ben Bacarisse wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:
pete wrote:
pereges wrote:I disagree. The code module I posted can be compiled and linked
Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:It makes more sense to prototype mergesort this way:
With CBFalconer's definition of a node, the code above will notNow, its working after I made some changes. Some changes
compile (p->data is a void *) so you have not shown us some key
code. Maybe the problem is in that part not shown.
head = mergesort(head, cmp);
int cmp(const void *vpa, constvoid *vpb)
{
node *va = ( node *)vpa;
node *vb = ( node *)vpb;
vector *a = (vector *) (va->data);
vector *b = (vector *) (vb->data);
if (a->coord[1] < b->coord[1])
return -1;
if (a->coord[1] >b->coord[1])
return 1;
else
return 0;
}
node *mergesort(node *, int (*)(node *, node *));
and to also make the corresponding changes
in the function definition.
Then you can write cmp without casts:
into whatever needs a mergesort.
Only if the data is indirect -- can be pointed to by a void *.
It doesn't need to know anything
about the data.
You can't use it (portably) to sort a list of ints where the int is
inside the node or indeed any node that contains the actual data. I
suspect this sort of thing matters to pete.
Other way.
You can use it to sort a list of pointers to any type.
OK, I need some help now... At first glance that is possible with
CBFalconer's compare function prototype.
/*
** sort a list of pointers to vector
*/
int cmp(const node *vpa, const node *vpb)
{
const vector *const a = vpa -> data;
const vector *const b = vpb -> data;
return b->coord[1] > a->coord[1] ? -1
: b->coord[1] != a->coord[1];
}
/*
** sort a list of pointers to long double
*/
int cmp(const node *vpa, const node *vpb)
{
const long double *const a_Lf_ptr = vpa -> data;
const long double *const b_Lf_ptr = vpb -> data;
return *b_Lf_ptr > *a_Lf_ptr ? -1 : *b_Lf_ptr != *a_Lf_ptr;
}
/*
** sort a list of pointers to string representations of long unsigned
*/
int cmp(const node *vpa, const node *vpb)
{
const long unsigned a_num = strtoul(vpa -> data, NULL, 10);
const long unsigned b_num = strtoul(vpb -> data, NULL, 10);
return b_num > a_num ? -1 : b_num != a_num;
}
All of these are possible with CBFalconer's cmp function prototype are
they not? Have I missed a flaw with it?
If nodes that contain the data (my example) are not the reason to
prefer a node *, what is?
--
Ben.
.
- References:
- Re: Using a link list over an array.
- From: CBFalconer
- Re: Using a link list over an array.
- From: pereges
- Re: Using a link list over an array.
- From: Ben Bacarisse
- Re: Using a link list over an array.
- From: pereges
- Re: Using a link list over an array.
- From: pete
- Re: Using a link list over an array.
- From: CBFalconer
- Re: Using a link list over an array.
- From: Ben Bacarisse
- Re: Using a link list over an array.
- From: pete
- Re: Using a link list over an array.
- Prev by Date: Re: Using a link list over an array.
- Next by Date: Re: Order of execution question
- Previous by thread: Re: Using a link list over an array.
- Next by thread: Re: Using a link list over an array.
- Index(es):
Relevant Pages
|