The only way I see to access all nodes (if the keys are not known) is twalk
. Is it allowed to use tdelete
inside twalk
? If not--how delete all nodes? (I don't wan't to use the non-portable GNU extension tdestroy
.)
No, you don't need to use twalk
, you can use tdelete
with a comparison function (the same function used to insert), tdelete
changes the root node, so passing and deleting while (root != NULL)
will do the trick, something like:
typedef struct {
int key;
char value[50];
} t_data;
static int comp(const void *pa, const void *pb)
{
const t_data *a = pa, *b = pb;
if (a->key > b->key) return +1;
if (a->key < b->key) return -1;
return 0;
}
int main(void)
{
void *root = NULL;
t_data *data;
...
while (root != NULL) {
data = *(t_data **)root;
tdelete(data, &root, comp);
free(data);
}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With