From d197d6421c317145e2aff89dd41de9d03eeaa00b Mon Sep 17 00:00:00 2001 From: nsz Date: Sun, 13 May 2012 01:34:20 +0200 Subject: [PATCH] search: add tdestroy (gnu extension) --- include/search.h | 2 ++ src/search/tdestroy.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/search/tdestroy.c diff --git a/include/search.h b/include/search.h index 3fa41adc..7c4fc583 100644 --- a/include/search.h +++ b/include/search.h @@ -38,6 +38,8 @@ struct qelem { struct qelem *q_forw, *q_back; char q_data[1]; }; + +void tdestroy(void *, void (*)(void *)); #endif #ifdef __cplusplus diff --git a/src/search/tdestroy.c b/src/search/tdestroy.c new file mode 100644 index 00000000..6f430bda --- /dev/null +++ b/src/search/tdestroy.c @@ -0,0 +1,21 @@ +#define _GNU_SOURCE +#include +#include + +struct node { + void *key; + struct node *left; + struct node *right; +}; + +void tdestroy(void *root, void (*freekey)(void *)) +{ + struct node *r = root; + + if (r == 0) + return; + tdestroy(r->left, freekey); + tdestroy(r->right, freekey); + freekey(r->key); + free(r); +} -- 2.25.1