From 20c79049e9b0a6a1aa47b1b9dacfbb31b6e7c992 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 6 Jul 2020 17:26:10 -0400 Subject: Add clear & free functions for maps --- collections/map/map.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'collections/map/map.c') diff --git a/collections/map/map.c b/collections/map/map.c index 712a895..beb8f7f 100644 --- a/collections/map/map.c +++ b/collections/map/map.c @@ -34,3 +34,47 @@ map *root; return map_size(root->left) + map_size(root->right) + 1; } + +void map_clear(root) +map *root; +{ + map *l, *r; + + l = root->left; + r = root->right; + + if (!root) + return; + + if (root->parent) { + free(root->key); + root->key = NULL; + + free(root->val); + root->val = NULL; + + root->parent = NULL; + + free(root); + } + + map_clear(l); + map_clear(r); +} + +void map_free(root) +map *root; +{ + if (!root) + return; + + root->key = NULL; + + map_free(root->left); + root->left = NULL; + + map_free(root->right); + root->right = NULL; + + free(root); +} -- cgit v1.1