Compare Generic Type

M

mott747

I'm working to learn C. I've into a problem that I'm not sure how to
solve. I'm implementing a basic binary tree but I don't want it to be
restricted to a particular data type. I seemed to be able to get
around this by making the data type stored a pointer to void. But then
this introduces the problem that I can't compare the values being
pointed to. Can anyone give me any tips?
 
D

Dave Vandervies

I'm working to learn C. I've into a problem that I'm not sure how to
solve. I'm implementing a basic binary tree but I don't want it to be
restricted to a particular data type. I seemed to be able to get
around this by making the data type stored a pointer to void. But then
this introduces the problem that I can't compare the values being
pointed to. Can anyone give me any tips?

Let the user give you a comparison function, the way qsort and bsearch do.

For bonus points, use a comparison function with the same interface,
so the same function can be used for all three.


dave
 
M

Martin Ambuhl

I'm working to learn C. I've into a problem that I'm not sure how to
solve. I'm implementing a basic binary tree but I don't want it to be
restricted to a particular data type. I seemed to be able to get
around this by making the data type stored a pointer to void. But then
this introduces the problem that I can't compare the values being
pointed to. Can anyone give me any tips?

struct cell {
int type; /* pointer to a property list is even better */
void *valueptr; /* this can, of course, hold a pointer to
struct cell */
}

struct leaf {
struct cell *cdr;
struct cell *car;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top