From 91eacbb8c734e26a9d17ba607000d9ad2089ff4b Mon Sep 17 00:00:00 2001 From: Dendy Date: Sun, 24 Jan 2021 17:40:31 +0100 Subject: [PATCH] Implement branch deleting with reparent --- src/main.c | 24 ++++-------------------- src/node.c | 11 +++++++++++ src/node.h | 1 + 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/main.c b/src/main.c index b7ce697..2daafbf 100644 --- a/src/main.c +++ b/src/main.c @@ -41,29 +41,13 @@ int main(int argc, char* *argv){ NOD_NodeAddChild(bsd, openbsd); NOD_NodeAddChild(bsd, debianbsd); + NOD_DestroyNodeBranch(bsd); + NOD_DestroyNodeBranch(ubuntu); + //NOD_NodeUnparent(child2); NOD_PrintNodeTree(distros); - NOD_PrintNodeTree(arch); - NOD_PrintNodeTree(linoox); - NOD_PrintNodeTree(debian); - NOD_PrintNodeTree(freebsd); - NOD_DestroyNode(distros); - NOD_DestroyNode(linoox); - NOD_DestroyNode(debian); - NOD_DestroyNode(ubuntu); - NOD_DestroyNode(mint); - NOD_DestroyNode(devuan); - NOD_DestroyNode(kali); - NOD_DestroyNode(voidlinux); - NOD_DestroyNode(arch); - NOD_DestroyNode(manjaro); - NOD_DestroyNode(parabola); - NOD_DestroyNode(endeavouros); - NOD_DestroyNode(bsd); - NOD_DestroyNode(freebsd); - NOD_DestroyNode(openbsd); - NOD_DestroyNode(debianbsd); + return 0; } diff --git a/src/node.c b/src/node.c index eb18aa1..2e2b215 100644 --- a/src/node.c +++ b/src/node.c @@ -20,10 +20,21 @@ NOD_Node *NOD_CreateNode(char *name){ void NOD_DestroyNode(NOD_Node *node){ if(node != NULL){ + printf("[ INF ] Deleting node \"%s\"\n", node->name); + NOD_NodeUnparent(node); free(node); } } +void NOD_DestroyNodeBranch(NOD_Node *node){ + if(node != NULL){ + for(int i = 0; i < node->childc && node->childv != NULL; i++){ + NOD_DestroyNodeBranch(node->childv[i]); + } + NOD_DestroyNode(node); + } +} + void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){ if(parent != NULL && child != NULL){ /** Prevent recursion */ diff --git a/src/node.h b/src/node.h index d2c6ac8..fbab22f 100644 --- a/src/node.h +++ b/src/node.h @@ -15,6 +15,7 @@ typedef struct node{ NOD_Node *NOD_CreateNode(char *name); void NOD_DestroyNode(NOD_Node *node); +void NOD_DestroyNodeBranch(NOD_Node *node); void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child); void NOD_NodeUnparent(NOD_Node *child);