Re: Using a link list over an array.



pete <pfiland@xxxxxxxxxxxxxx> writes:

Ben Bacarisse wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:

pete wrote:
pereges wrote:
Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:

With CBFalconer's definition of a node, the code above will not
compile (p->data is a void *) so you have not shown us some key
code. Maybe the problem is in that part not shown.
Now, its working after I made some changes. Some changes
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;
}
It makes more sense to prototype mergesort this way:

node *mergesort(node *, int (*)(node *, node *));

and to also make the corresponding changes
in the function definition.

Then you can write cmp without casts:
I disagree. The code module I posted can be compiled and linked
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.
.



Relevant Pages

  • Re: Writing a program to sort numbers - HELP!
    ... You may not have a choice but strtol is preferred to atoi and fgets is ... Then we using a sort function to sort the ... >int main ... probably meant *u and *v to dereference the pointers you received from ...
    (comp.lang.c.moderated)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • Re: void *
    ... list_add_head(&p, (int *)100); ... That was my understanding of 'void *' concept. ... In fact at your level the first thing you should assume when you appear to need a cast is that you have done something wrong and a cast is *not* the solution. ... Personally I don't think you understand the concept of pointers yet, so you need to read the sections of your text book and the comp.lang.c FAQ that refer to pointers. ...
    (comp.lang.c)
  • Re: problem with array
    ... > different sort algorythms. ... > the sort the array elements are compared and swapped. ... void selectionsort(void *base, size_t n, size_t size, ... void PrintArrays(int *a, int **pa,size_t n); ...
    (comp.lang.c)
  • Re: How are C++ objects laid out in memory ?
    ... > void print(); ... int main ... > But the function pointers declared in above MsgStruct structure have ... > pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)