From bb1816299ea7e500673a8358da18b304c4ce5950 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 12 Jun 2018 17:03:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=92=8C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list/list.c | Bin 2172 -> 2166 bytes list/list.h | Bin 23946 -> 24428 bytes list/list_expand.h | Bin 21118 -> 22982 bytes main.c | 4 +- tree/tree.c | 6 +- tree/tree.h | 322 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 330 insertions(+), 2 deletions(-) diff --git a/list/list.c b/list/list.c index 2750dedebd3de9369e2a986d85a6b0c5823b4aba..c3b79f45cb66f0a11e1d0d032ee24ef4597381b6 100644 GIT binary patch delta 27 hcmew(@J(QY3^Sw8WO?RBM!(IAnNt~o6gO)jGXQ_S2l)U1 delta 33 kcmew+@JC>S3^Q{)L(XJb=0+f$w|PEuDkGS|%38<_0JAO$Z~y=R diff --git a/list/list.h b/list/list.h index 1de859a1637c99ec5846549e671e7ba31e0b881f..adc3808f6fc848355fea0904ee9f8fc24a2ad301 100644 GIT binary patch delta 246 zcmeC$&G=>?;|3m9MxV+2th$VTo9$UovQI8E7Gw2i$YaQv>>r`N`4i7Pes({Ge1;T; z)X5)JL^rRKImXE7H+gM@vARIM$qV!ZCYu-w ROn&7qG&#&zVRBWh0022XO>O`H delta 164 zcmaE}kFjev;|3m9=6Hsj$-Jz(Kss-;HS0N>bm-6x= fnaQ9zSuk9Fa)B(@W{pS{Ca@ac=whbHG4TQb!fh}D diff --git a/list/list_expand.h b/list/list_expand.h index 022e003ac94164e3e775cb19cd382249d9242c54..0b234a4ca9c71cea19b88402309369ae491e641e 100644 GIT binary patch delta 682 zcmZ`%&ubGw6n+z%GzpXKCe4pcRl1?6ByE%mrBtLFq}qyEcAI*TLaB(UxH+U)R_PB) z`w#T=^&p-q^bZgQJPH=ki!`1~5sxAsJqcB3H_3!NJh{$Hv71~D1RZLSizhsF@jpC1ZxN^IQ<2wx47clxoUwk+ zFP6oAE-c>McpYj-kux$lveRA&=FFt{cKh8wVR2+H2F?Uy=UNXAvja>A%cQVmBv8oc zI8GIM8&s2u*6@31khvJlU&Y?Ax-CZ_Tbq(gCztL^87qF?Wiq_+P*M6cbRs<6soAN|wRI?yj~IWYRV(cGHw(b1{A zM2&PHnzI#Hb~NaH799(A5fgC@`?k{cGH1Qi*%soxIeeK@V3b=Lcr}fi^P1o3aXE=e MQ^i45MQwiI4_r{SivR!s delta 257 zcmX@MnepEe#tnQtlY5M%COh%OvHLOPGo&!2Zr;vg&&V9lkT-chfAD56z7jBJ^M3y0 zjGQG5MGQF%sX)1Y!AO{z$u>gw;Oso%LtwU{NF|WY*}PgLl}Qk4GJ^tx7K0`bR!^SD zuR8gHsKVq);+rNLsPQrCOg<id = getId(); + p_tnode->child_num = 0; + p_tnode->father = NULL; + p_tnode->if_malloc = 0; + p_tnode->value = NULL; + p_tnode->type = NULL; + p_tnode->home = initList(); + p_tnode->room = NULL; + return p_tnode; +} + +Tree *initTree(void) { + Tree *p_tree = (Tree *)malloc(sizeof(Tree)); + p_tree->id = getId(); + p_tree->root = NULL; + return p_tree; +} + +int *initMallocValueForTNode(TNode *p_tnode, char *type, void *value) { + p_tnode->type = type; + p_tnode->value = value; + p_tnode->if_malloc = 1; + return 0; +} + +int addChildInLeft(TNode *f_tnode, TNode *c_tnode) { + Node *p_node = initNode(); + initMalllocValueForNode(p_node, "pointer", c_tnode); + insertInHead(f_tnode->home, p_node); + c_tnode->father = f_tnode; + f_tnode->child_num++; + return 0; +} + +int addChildInRight(TNode *f_tnode, TNode *c_tnode) { + Node *p_node = initNode(); + initMalllocValueForNode(p_node, "pointer", c_tnode); + insertInTail(f_tnode->home, p_node); + c_tnode->father = f_tnode; + f_tnode->child_num++; + return 0; +} + +TNode *getBrotherInLeft(TNode *p_tnode) { + List *p_home = p_tnode->father->home; + Node *p_node =p_home->head; + int index = getIndexByNode(p_home, p_node); + if (index > 0) return findByIndexForNode(p_home, index -1); + return NULL; +} + +TNode *getBrotherInRight(TNode *p_tnode) { + List *p_home = p_tnode->father->home; + Node *p_node = p_home->head; + int index = getIndexByNode(p_home, p_node); + if (index < p_home->length-1) return findByIndexForNode(p_home, index + 1); + return NULL; +} + +int removeChildInLeft(TNode *p_tnode) { + TNode *c_tnode = p_tnode->home->head; + c_tnode->father = NULL; + releaseOnlyNode(c_tnode->room); + c_tnode->room = NULL; + p_tnode->child_num--; + popFromHead(p_tnode->home); +} + +int removeChildInRight(TNode *p_tnode) { + TNode *c_tnode = p_tnode->home->tail; + c_tnode->father = NULL; + releaseOnlyNode(c_tnode->room); + c_tnode->room = NULL; + p_tnode->child_num--; + popFromTail(p_tnode->home); +} + +unsigned long long target_id = 0; +TNode *target_value_id = NULL; +TNode*_dogetChildById(char *type, void *value); + +TNode *getChildById(TNode *p_tnode, unsigned long long id) { + List *p_home = p_tnode->home; + target_id = 0; + target_value_id = NULL; + listThrough(p_home, _dogetChildById); + if (target_value_id != NULL) { + return target_value_id; + } + return NULL; +} + +TNode*_dogetChildById(char *type, void *value) { + if (!strcmp(type, "pointer")) { + TNode *p_tode = (TNode *)value; + if (p_tode->id == target_id) { + target_value_id = p_tode; + return -1; + } + } + return 0; +} + +char *target_type = NULL; +void *target_value = NULL; +TNode *target_value_value = NULL; +TNode*_dogetChildByValue(char *type, void *value); + +TNode *getChildByValue(TNode *p_tnode, char *type, void *value) { + List *p_home = p_tnode->home; + target_value = value; + target_type = type; + target_value_value = NULL; + listThrough(p_home, _dogetChildByValue); + if (target_value_value != NULL) { + return target_value_value; + } + return NULL; +} + +TNode*_dogetChildByValue(char *type, void *value) { + if (!strcmp(type, target_type)) { + TNode *p_tode = (TNode *)value; + if (!strcmp(target_value, "int")) { + if (*(int *)p_tode->value == *(int *)target_value) + { + target_value_value = p_tode; + return -1; + } + } + else if (!strcmp(target_value, "double")) + { + if (*(double *)p_tode->value == *(double *)target_value) + { + target_value_value = p_tode; + return -1; + } + } + else if (!strcmp(target_value, "string")) + { + if (!strcmp((char *)p_tode->value,(char *)target_value)) + { + target_value_value = p_tode; + return -1; + } + } + else if (!strcmp(target_value, "pointer")) + { + if (p_tode->value == target_value) + { + target_value_value = p_tode; + return -1; + } + } + + } + return 0; +} + +int removeChildById(TNode *p_tnode, unsigned long long id) { + TNode *t_tnode = getChildById(p_tnode, id); + if (t_tnode != NULL) { + TNode *p_fnode = t_tnode->father; + p_fnode->child_num--; + removeById(p_fnode->home, t_tnode->room->id); + releaseOnlyNode(t_tnode->room); + t_tnode->room = NULL; + return 0; + } + return -1; +} + +int removeChildByValue(TNode *p_tnode, char *type, void *value) { + TNode *t_tnode = getChildByValue(p_tnode, type, value); + if (t_tnode != NULL) { + TNode *p_fnode = t_tnode->father; + p_fnode->child_num--; + removeById(p_fnode->home, t_tnode->room->id); + releaseOnlyNode(t_tnode->room); + t_tnode->room = NULL; + return 0; + } + return -1; +} + +TNode *getChildByIndex(TNode *p_tnode, unsigned long long index) { + List *p_home = p_tnode->home; + Node *p_node = p_home->head; + int m_index = 0; + if (index < p_tnode->child_num-1) + { + while (p_node != NULL) { + m_index++; + p_node = p_node->next; + } + return (TNode *)p_node->value; + } + return NULL; +} + +unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode) { + List *p_home = f_tnode->home; + Node *p_node = p_home->head; + int m_index = 0; + while (p_node != NULL) { + TNode *p_tnode = (TNode *)p_node->value; + if (p_tnode->id == c_tnode->id) { + return m_index; + } + m_index++; + p_node = p_node->next; + } + return NULL; +} + +int removeChildByIndex(TNode *p_tnode, unsigned long long index){ + TNode *t_tnode = getChildByIndex(p_tnode, index); + if (t_tnode != NULL) { + TNode *p_fnode = t_tnode->father; + p_fnode->child_num--; + removeById(p_fnode->home, t_tnode->room->id); + releaseOnlyNode(t_tnode->room); + t_tnode->room = NULL; + return 0; + } + return -1; +} + +int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + TNode *p_tnode = p_tree->root; + if (p_tnode != NULL) { + func(p_tnode, 0); + if (p_tnode->child_num > 0) { + for (int i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) { + break; + } + } + } + } + return 0; +} + +int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) { + if (p_tnode->child_num > 0) { + for (int i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughDown(getChildByIndex(p_tnode, i), height + 1, func)) return -1; + } + } + int func_back = func(p_tnode, height); + if (func_back == -1)return -1; + return 0; +} + +int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + TNode *p_tnode = p_tree->root; + if (p_tree->root != NULL) { + if (p_tree->root->child_num > 0) { + func(p_tnode, 0); + for (int i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) { + break; + } + } + } + } + return 0; +} + +int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) { + int func_back = func(p_tnode, height); + if (p_tnode->child_num > 0) { + for (int i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughDown(getChildByIndex(p_tnode, i), height + 1, func)) return -1; + } + } + if (func_back == -1)return -1; + return 0; +} +