From 6dbd74f199546ecd05c32c89a4572121b8b5abde Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 12 Jun 2018 19:07:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list/list.c | Bin 2166 -> 20414 bytes list/list.h | Bin 24778 -> 3888 bytes list/list_expand.c | 328 +++++++++++++++++++++++++++++++ list/list_expand.h | Bin 22982 -> 3374 bytes main.c | 12 -- stack/stack.c | Bin 588 -> 2886 bytes stack/stack.h | Bin 4426 -> 1276 bytes stack/stack_expand.c | 51 +++++ stack/stack_expand.h | Bin 3760 -> 838 bytes test.c | 63 ++++++ test.h | 15 ++ tree/tree.c | 439 ++++++++++++++++++++++++++++++++++++++++- tree/tree.h | 458 ++----------------------------------------- 13 files changed, 911 insertions(+), 455 deletions(-) create mode 100644 list/list_expand.c delete mode 100644 main.c create mode 100644 stack/stack_expand.c create mode 100644 test.c create mode 100644 test.h diff --git a/list/list.c b/list/list.c index c3b79f45cb66f0a11e1d0d032ee24ef4597381b6..e434c5a7b85155ae381b82bd6411fdc32bc6d9ba 100644 GIT binary patch literal 20414 zcmeHP%Who95$$Cd!2d92kYFfTp%o+uoEKmNF)To45Ll6y0!57^CZpki98qy(En;L7 z7=f4mmV80JAgiqN1zG1&r)$nsb#>pzNR-Uj!hqz=y;arquIjqY`R{-B)0H%lpI7^O zBYiusI!U+E!*ra^d7jJNy8<`K zyx`6%WBHX^avsUrkO0u2@QM8XD1Uw|cNTK>ROlbcRlrQr57OJ|C-U<{d4H0ncwb=d z%BLU8{fC95uhyRXS;x@xcZB1G@H|hCvoKTR`Dz#R(|B;S+wPcxOBd~K2dqxUB1=kl7Q>jjj-E!I@;YgNl1h=N6Wko{6|-yV<#SS!&BvOsG!~oDe^qBC zJmW-h434n~Xz1iHul$kN19rp}`mjdlP|Ok<)nzGKp#S=qIatRFVW?}>iO2F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-nb!U!K z8|?hBNt4emWsaB^*{!Kv`qsoS)2pZ+lcx=~wp$Sl{vw? Kd&sP)cK-!hHOkun diff --git a/list/list_expand.c b/list/list_expand.c new file mode 100644 index 0000000..a5f8fb3 --- /dev/null +++ b/list/list_expand.c @@ -0,0 +1,328 @@ +#include "list_expand.h" + +Node *nodeWithInt(int m_int) { + int *p_int = (int *)malloc(sizeof(int)); + *p_int = m_int; + Node *p_node = initNode(); + initMallocValueForNode(p_node, "int", (void *)p_int); + return p_node; +} + +Node *nodeWithDouble(double m_double) { + double *p_double = (double *)malloc(sizeof(double)); + *p_double = m_double; + Node *p_node = initNode(); + initMallocValueForNode(p_node, "double", (void *)p_double); + return p_node; +} + +Node *nodeWithString(const char *m_string) { + char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1)); + strcpy_s(p_string, sizeof(p_string), m_string); + Node *p_node = initNode(); + initMallocValueForNode(p_node, "string", (void *)p_string); + return p_node; +} + +Node *nodeWithPointer(void *m_pointer) { + Node *p_node = initNode(); + initMallocValueForNode(p_node, "pointer", m_pointer); + return p_node; +} + +Node *nodeWithComplex(void) { + Node *p_node = initNode(); + p_node->type = "list"; + p_node->value = initList(); + p_node->if_malloc = 1; + return p_node; +} + +Node *findByIndexForNode(List *p_list, unsigned long long m_index) { + Node *p_node = p_list->head; + for (unsigned long long i = 0; i < m_index; i++) { + p_node = p_node->next; + } + return p_node; +} + +int listThrough(List *p_list, int(*p_func)(const char *, void *)) { + Node *p_node = p_list->head; + while (p_node != NULL) { + if (p_node->if_malloc == 1) { + int m_return = (*p_func)(p_node->type, p_node->value); + if (m_return == -1) break; + else if (m_return == 1) { + p_node = p_node->last; + continue; + } + else { + + } + } + p_node = p_node->next; + } + return 0; +} + +int getByIntForNode(Node *p_node) { + if (!strcmp(p_node->type, "int")) return *(int *)(p_node->value); + else return -1; +} + +char *getByStringForNode(Node *p_node) { + if (!strcmp(p_node->type, "string")) return (char *)(p_node->value); + else return NULL; +} + +double getByDoubleForNode(Node *p_node) { + if (!strcmp(p_node->type, "double")) return *(double *)(p_node->value); + else return -1; +} + +void *getByPointerForNode(Node *p_node) { + if (!strcmp(p_node->type, "pointer")) return (void *)(p_node->value); + else return NULL; +} + +void printListInfo(List *p_list, int priority) { + for (int i = 0; i < priority; i++) printf(" "); + printf("###LIST(location:%p, id:%llu){\n", p_list, p_list->id); + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("HEAD->%p / Tail->%p / Length:%llu\n", p_list->head, p_list->tail, p_list->length); + Node *p_node = p_list->head; + int i = 0; + while (p_node != NULL) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("%d.... \n", i); + printNodeInfo(p_node, priority + 1); + p_node = p_node->next; + i++; + } + for (int i = 0; i < priority; i++) printf(" "); + printf("}\n"); + +} + +void printList(List *p_list) { + Node *p_node = p_list->head; + printf("["); + int if_nearLast = 0; + while (p_node != NULL) { + if (!if_nearLast && p_node->next == NULL) if_nearLast = 1; + if (!strcmp(p_node->type, "int")) { + printf("%d", *(int *)(p_node->value)); + } + else if (!strcmp(p_node->type, "double")) { + printf("%a", *(double *)(p_node->value)); + } + else if (!strcmp(p_node->type, "string")) { + printf("%s", (char *)(p_node->value)); + } + else if (!strcmp(p_node->type, "pointer")) { + printf("%s", (char *)(p_node->value)); + } + else if (!strcmp(p_node->type, "list")) { + printList((List *)p_node->value); + } + if (!if_nearLast) { + printf(", "); + } + p_node = p_node->next; + } + printf("]"); +} + +void printNodeInfo(Node *p_node, int priority) { + for (int i = 0; i < priority; i++) printf(" "); + printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id); + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc); + if (!strcmp(p_node->type, "int")) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(Int):%d\n", *(int *)(p_node->value)); + } + else if (!strcmp(p_node->type, "double")) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(Double):%a\n", *(double *)(p_node->value)); + } + else if (!strcmp(p_node->type, "string")) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(String):%s\n", (char *)(p_node->value)); + } + else if (!strcmp(p_node->type, "pointer")) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(Pointer):%s\n", (char *)(p_node->value)); + } + else if (!strcmp(p_node->type, "list")) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(List):\n"); + printListInfo((List *)p_node->value, priority + 2); + } + for (int i = 0; i < priority; i++) printf(" "); + printf("}\n"); +} + +void printNode(Node *p_node) { + printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id); + printf(" "); + printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc); + printf(" "); + if (!strcmp(p_node->type, "int")) { + printf("%d", *(int *)(p_node->value)); + } + else if (!strcmp(p_node->type, "double")) { + printf("%a\n", *(double *)(p_node->value)); + } + else if (!strcmp(p_node->type, "string")) { + printf("%s\n", (char *)(p_node->value)); + } + else if (!strcmp(p_node->type, "pointer")) { + printf("%s\n", (char *)(p_node->value)); + } + else if (!strcmp(p_node->type, "list")) { + printList((List *)p_node->value); + } + printf("}\n"); +} + + +Node *findByIntForNode(List *p_list, int target) { + int *p_target = (int *)malloc(sizeof(int)); + *p_target = target; + Node *t_node = findByValue(p_list, "int", p_target); + free(p_target); + return t_node; +} + +Node *findByDoubleForNode(List *p_list, double target) { + double *p_target = (double *)malloc(sizeof(double)); + *p_target = target; + Node *t_node = findByValue(p_list, "double", p_target); + free(p_target); + return t_node; +} + +Node *findByStringForNode(List *p_list, char *target) { + char *p_temp = (char *)malloc(sizeof(char)*(strlen(target) + 1)); + strcpy_s(p_temp, sizeof(p_temp), target); + Node *t_node = findByValue(p_list, "string", p_temp); + free(p_temp); + return t_node; +} + +Node *findByPointerForNode(List *p_list, void *target) { + Node *t_node = findByValue(p_list, "pointer", target); + return t_node; +} + +int addValueForComplex(Node * p_node, char *type, void *value) { + if (!strcmp(p_node->type, "list")) { + List *c_list = (List *)p_node->value; + Node *c_node = initNode(); + initMallocValueForNode(c_node, type, value); + insertInTail(c_list, c_node); + return 0; + } + return -1; +} + +int addIntForComplex(Node *p_node, int temp) { + if (!strcmp(p_node->type, "list")) { + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + addValueForComplex(p_node, "int", p_temp); + return 0; + } + return -1; +} + +int addDoubleForComplex(Node *p_node, double temp) { + if (!strcmp(p_node->type, "list")) { + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + addValueForComplex(p_node, "double", p_temp); + return 0; + } + return -1; +} + +int addStringForComplex(Node *p_node, char *temp) { + if (!strcmp(p_node->type, "list")) { + char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1)); + strcpy_s(p_temp, sizeof(p_temp), temp); + addValueForComplex(p_node, "string", p_temp); + return 0; + } + return -1; +} + +int addPointerForComplex(Node *p_node, void *temp) { + if (!strcmp(p_node->type, "list")) { + addValueForComplex(p_node, "pointer", temp); + return 0; + } + return -1; +} + +List *m_findByInt(List* p_list, int temp) { + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + List *t_list = mply_findByValue(p_list, "int", (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByDouble(List* p_list, double temp) { + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + List *t_list = mply_findByValue(p_list, "double", (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByString(List* p_list, char *temp) { + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + strcpy_s(p_temp, sizeof(p_temp), temp); + List *t_list = mply_findByValue(p_list, "string", (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByPointer(List* p_list, void *temp) { + List *t_list = mply_findByValue(p_list, "double", (void *)temp); + return t_list; +} + +unsigned long long getIndexByNode(List *p_list, Node *p_node) { + Node *t_node = p_list->head; + unsigned long long index = 0; + while (t_node != NULL) { + if (p_node->id == t_node->id) return index; + index++; + t_node = t_node->next; + } + return 0; +} + +List *m_findByIntForNode(List* p_list, int temp) { + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + return mply_findByValue(p_list, "int", (void *)p_temp); +} + +List *m_findByDoubleForNode(List* p_list, double temp) { + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + return mply_findByValue(p_list, "double", (void *)p_temp); +} + +List *m_findByStringForNode(List* p_list, char *temp) { + char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1)); + strcpy_s(p_temp, sizeof(p_temp), temp); + return mply_findByValue(p_list, "string", (void *)p_temp); +} + +List *m_findByPointerForNode(List* p_list, void *temp) { + return mply_findByValue(p_list, "pointer", (void *)temp); +} diff --git a/list/list_expand.h b/list/list_expand.h index 0b234a4ca9c71cea19b88402309369ae491e641e..e2f32e93f8cb09789ef0ab4230bd9d77b18a929c 100644 GIT binary patch delta 20 ccmX@MnQ@)ehDAJ+7YT?>Uc-|%d0V6a0A1e*UjP6A literal 22982 zcmdU1-)~%35k4vs#YlODC&VUJt96o=l()745pb(o#EzRy`a>x-&U#~8Bpc&jB!m_z zicqA2RHRBsNJzX?;)Oqee}D)019;{EUXXa=4bd{+o&EOAoO|}}-RoT^mX)r(_uMmc zX1bNK%Ee|2$n+?IPKXA?4!3I9p)ri z%+-^Z!=+!*c&QeN^`u%$H(jg8WIegollA4MxV=@x!bbS6Urr zQ3w_@A8R{GiUZr23$`qi%tt+#go*&dr_9SFdh8esuWHkN*7O->=;L;V7v@Y?R7x zkdq*_FY_aN)sqT5jh9Na`86`LQTuT^w;f-zy4snDaLlF=(UN@H6MHc?m*dFVfOGxl z@YC*Z)-GJt6>4R*c(=1wMNHkfEvBQsRWVU_=7_$t#uC-2uRC|03Pwu-b6OYS2}aSy zvM2ZbRN~{VWFd~zHA1hsfpzR`QMP(vZs^O@PptsyxEJ|~>uVPl)Y*A$d7zVOm9q4B zr;YmavXfK2&PcXO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QcnvG}{)xJ&cW}-%eiz??!1s1W)_5$=1YOcRt?S$vqF`>7r zBL@28Y->blv>mVIDq3?M#Q#SQCDJ;|hiDCN`QzX^^(?$t>O<7Cv&|8CYx@y-o2O%% z@RZ}Szty!q7pdn3%N|Dc?{mzb)m@*0anBMFZ?8{_M?e8^W#~4eEB=2MxmB - -int stack(void); -int list(void); -int tree(void); - -int main(int argc, char **argv) { - list(); - tree(); - getchar(); - return 0; -} \ No newline at end of file diff --git a/stack/stack.c b/stack/stack.c index 15364d1a423506b693b8038df98cc5b42fcada4b..32742ca3a3c9f2e20eb15cc713ab17597f1e3489 100644 GIT binary patch literal 2886 zcmbVO%We}v5bUdw_y+?Q8xaI&1V`7E@69xsMWUWZU|!{)Hg%k9Ia5`gg*(6Ol`?@wRGq0-n30u4wM<~yVj3vRsTVQK zxzyRFU9?5~YpB||%V$oC<}>(=G2pq%?Yc1Gc!SfpO0z4@?2(cwVlvDhbEcJ~e(YGy zHO^3qG$*3XFJi2A8fh6AIu+?LBz04Cn2ejixFO+dk2vve-@$PcsxakQg!|~N9b}R5 zn3FxvbE5C7_sTK99I?ew@j5fo{I=()gtB9F@5bEZh?SLiMY~irHS4r9lmUyq4jQ~D zCY;s!znx<^4qH6h-e=q5mO7grthvwRY1*p&K`XE=4qHf?`TCmKNz>Z+m8p`PjH}F_3w= z_r3FO^7q?Qs-w0#=%k$@dGa;VScQx>)c0Cb{n7`wBZ`p{6c6e;pFv+M1AQu^zEep3 z>{J~ss!pCBd0saho}N)!f|NJ4GN8k>^vCqfQ3ch+l$-TU&{(&p8Rx0G4>V{Ibf7;m zHEB&~lMLxlz>&EWR*omp8x`XwhYg~iyOQPQUNLig^_lO>(!7(q(URQlw~6$k?~>=$ vC9=@Gqz%|F<23VYZ)fT2M6digT@RcMn@2a@*Kc7R2AZ5)F8SL(;nMFPz|C0; diff --git a/stack/stack.h b/stack/stack.h index d06f43e50f5ad31c0ee554f10ad609ef76ae2b3c..08b406f5d5717b50af8ac96ec3c3b7674251bd31 100644 GIT binary patch delta 11 ScmX@5^oMi91(wNjf;s>n=>%i| literal 4426 zcmd5=$!-%t5Un#3|1c;QJ0dJ+76~CC5a5d;*T~p$Y+>w?Jq}BhUkBdncF|ou)8ny~ z1F|B|QtNxQwDafpiOgjwV_C>nCUPl-JeQ4BxL)AR7kMNzc`5hhp8KQp6!MguLUt&h z`t z#B$}lOz>m@xh3x7t2s2E89hct56t31zR3uB)Ep;rV`q&~6?iv;+!|M>xc6~etJjdG zFG^fHn_J{&W%g%CZDRHVUcBxtEDmJ=>%_@FI|RKOll!rlb<P2%Q!#1B1_Dhq0z#>GUn5iAjutc|Pz)H|z z7AjrWXN0N$I7_w9IMv4*?6U$!@N^kjZ}+5@a#jL+UE{HN(QdwSY6`h{L*{e&h3r?DEwOB0G*6vg*7_OdtDhe9as>;OyhF$LI_Sk+R|xf=B;8l{Rk>>N z(gXP{AMmjqOExQ1%^LU>yBb$4v~$URy;1|>fjh1CzNCn&OsGNaBp)47jAiyAuCh7A z)%u}`TfKC1wBa6W>TGXA-p&)o8RkQqUK3;LyPdH^XN%>Kd$gBS2&2{-d*EtPt1PVm z?FY-U#lUBst74y<+R^WvbZkq}VYJFjo3$qSHb(NX*q%Le2t7?YP__5-s*<;6*K-VJ}ZJ-B8Q&M^*c@tbGO&E$9eMiR*viJg+VrsMJ?M1w7Pw~D$}I5 zzmLg5d`1}{6P~M?n~f{o*S5Fn*oEz_%-Xo)YSGC^7Hl`>b!>$-ZTYF|bDs;@R*z{W z$JSxMD(&F7Q_}gata+TtP#&o?gSbD#4ovfD@^I{a4qkkHwXqXNr@sl?v~t;{`E=z+ z&sqP&uNFIdx-*jQyt<}ER`2YDsB5J>hI^}Nu?{EaFqKa*9XNA%$|>YAey!R6P0VnA Jiszi*e*tWWm}LL} diff --git a/stack/stack_expand.c b/stack/stack_expand.c new file mode 100644 index 0000000..faea8b1 --- /dev/null +++ b/stack/stack_expand.c @@ -0,0 +1,51 @@ +#include "stack_expand.h" + +SNode *snodeWithInt(int temp) { + SNode *p_snode = initSNode(); + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + initMallocValueForSNode(p_snode, "int", p_temp); + return p_snode; +} + +SNode *snodeWithDouble(double temp) { + SNode *p_snode = initSNode(); + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + initMallocValueForSNode(p_snode, "double", p_temp); + return p_snode; +} + +SNode *snodeWithString(char *temp) { + SNode *p_snode = initSNode(); + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + strcpy_s(p_temp, strlen(p_temp), temp); + initMallocValueForSNode(p_snode, "string", p_temp); + return p_snode; +} + +SNode *snodeWithPointer(void *temp) { + SNode *p_snode = initSNode(); + initMallocValueForSNode(p_snode, "pointer", temp); + return p_snode; +} + +int getValueByIntForSNode(SNode *p_snode) { + if (!strcmp(p_snode->type, "int")) return *(int *)p_snode->value; + else return -1; +} + +double getValueByDoubleForSNode(SNode *p_snode) { + if (!strcmp(p_snode->type, "double")) return *(double *)p_snode->value; + else return -1; +} + +char *getValueByStringForSNode(SNode *p_snode) { + if (!strcmp(p_snode->type, "int")) return (char *)p_snode->value; + else return NULL; +} + +void *getValueByPointerForSNode(SNode *p_snode) { + if (!strcmp(p_snode->type, "int")) return (void *)p_snode->value; + else return NULL; +} \ No newline at end of file diff --git a/stack/stack_expand.h b/stack/stack_expand.h index fe14aa2a308bec03fba7bd35e52a8e355c9664d1..300b68efb06b1eb147a2e3b461a5e23cdbe412f2 100644 GIT binary patch delta 11 ScmdlWdyH)Z5A){4eJ zY!B&IJRZ_#51;T7ukjvzyumjd@U)Lzp5B$dy`|(LIb3HiF?#fHTISqkCe!ltm6;dJ zdXK*p$Xp!-I(X_LUH8;3y)5ya{TT7tT%DSF=&9~y?9BuNb~8b@aSjKJDxD~p!<<>1 zxpQuONC&)4f-&t1d|@1!;s~#BURM8>PtH}RGWA_&r0cBrr~A^>tFFB+N8PEWK6NkZ zeA?bM^?GwWC2l$=m~pBX4p|B3BOi;f88IHQncz7sWNw1dXqm0B5xj)ib{V0+ishdV znD!GqadSWBq=&@Hf}Vd71E=I&uRj%2qFODit_I|8hKX9mGr<O&g%A{A?%HIQQ zF70(JsFp;j!&8q_3~vkn8>;M{Xa`mM_>2$yRFT#0&D2h_9b|ca)nm#k=Vm*#<#t|9 izRSDe6XMEmFaIXl=f6M6{{eL>=dPRZ-!84>j{X+}L{?=0 diff --git a/test.c b/test.c new file mode 100644 index 0000000..c21a0f5 --- /dev/null +++ b/test.c @@ -0,0 +1,63 @@ +#include "test.h" + +int list(void) { + init_rand(); + safeModeForNode(1); + List *t_list = initList(); + + /*for(int i = 0; i < 9; i++){ + Node *t_node = initNode(); + int *t_i = (int *)malloc(sizeof(int)); + *t_i = i; + initMallocValue(t_node,"int",(void *)t_i); + insertInTail(t_list,t_node); + }*/ + + /*Node *t_node = initNode(); + insertInTail(t_list,t_node); + initMalllocValue(t_node,(void *)"there");*/ + + for (int i = 0; i < 12; i++) { + insertInHead(t_list, nodeWithInt(i)); + insertInTail(t_list, nodeWithInt(i)); + } + + + + printListInfo(t_list, 0); + printList(t_list); + List *m_list; + m_list = m_findByIntForNode(t_list, 5); + printList(m_list); + printf("\n"); + + releaseAllForNode(); + + return 0; +} + +int tree(void) { + safeModeForTree(1); + Tree *t_tree = initTree(); + releaseAllForTree(); + return 0; +} + +int stack(void) { + Stack *t_stack = initStack(); + for (int i = 0; i < 10; i++) { + pushStack(t_stack, snodeWithInt(i)); + } + for (int i = 0; i < 10; i++) { + printf("%d", getValueByIntForSNode(popStack(t_stack))); + } + releaseStack(t_stack); + return 0; +} + + +int main(int argc, char **argv) { + tree(); + getchar(); + return 0; +} \ No newline at end of file diff --git a/test.h b/test.h new file mode 100644 index 0000000..cba9876 --- /dev/null +++ b/test.h @@ -0,0 +1,15 @@ +#ifndef TEST_H +#define TEST_H + +#include + +#include "list/list_expand.h" +#include "stack/stack_expand.h" +#include "tree/tree.h" + +int stack(void); +int list(void); +int tree(void); + +#endif // TEST_H + diff --git a/tree/tree.c b/tree/tree.c index 479d186..8d981e8 100644 --- a/tree/tree.c +++ b/tree/tree.c @@ -1,5 +1,438 @@ #include"tree.h" -int tree(void) { - + +int safeModeForTree(int ifon) { + if (ifon == 1) { + if (tnode_list == NULL && tree_list == NULL) { + tnode_list = (List *)malloc(sizeof(List)); + tree_list = (List *)malloc(sizeof(List)); + + tree_list->head = NULL; + tree_list->length = 0; + tree_list->tail = NULL; + + tnode_list->head = NULL; + tnode_list->length = 0; + tnode_list->tail = NULL; + + if_safeModeForTree = 1; + } + else { + return -1; + } + } + + return ifon; +} + +TNode *initTNode(void) { + TNode *p_tnode = (TNode *)malloc(sizeof(TNode)); + p_tnode->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; + if (if_safeModeForTree) { + if (if_safeModeForNode) { + if_safeModeForNode = 0; + Node *s_node = initNode(); + initMallocValueForNode(s_node, "pointer", (void *)p_tnode); + insertInTail(tnode_list, s_node); + if_safeModeForNode = 1; + } + else + { + Node *s_node = initNode(); + initMallocValueForNode(s_node, "pointer", (void *)p_tnode); + insertInTail(tnode_list, s_node); + } + } + return p_tnode; +} + +Tree *initTree(void) { + Tree *p_tree = (Tree *)malloc(sizeof(Tree)); + p_tree->id = getId(); + p_tree->root = NULL; + if (if_safeModeForTree) { + if (if_safeModeForNode) { + if_safeModeForNode = 0; + Node *s_node = initNode(); + initMallocValueForNode(s_node, "pointer", (void *)p_tree); + if_safeModeForNode = 1; + insertInTail(tree_list, s_node); + } + else + { + Node *s_node = initNode(); + initMallocValueForNode(s_node, "pointer", (void *)p_tree); + insertInTail(tree_list, s_node); + } + } + 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; -} \ No newline at end of file +} + +int addChildInLeft(TNode *f_tnode, TNode *c_tnode) { + Node *p_node = initNode(); + initMallocValueForNode(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(); + initMallocValueForNode(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; + unsigned long long index = getIndexByNode(p_home, p_node); + if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value); + return NULL; +} + +TNode *getBrotherInRight(TNode *p_tnode) { + List *p_home = p_tnode->father->home; + Node *p_node = p_home->head; + unsigned long long index = getIndexByNode(p_home, p_node); + if (index < p_home->length - 1) return (TNode *)(findByIndexForNode(p_home, index + 1)->value); + return NULL; +} + +int removeChildInLeft(TNode *p_tnode) { + TNode *c_tnode = (TNode *)p_tnode->home->head->value; + c_tnode->father = NULL; + releaseOnlyNode(c_tnode->room); + c_tnode->room = NULL; + p_tnode->child_num--; + popFromHead(p_tnode->home); + return 0; +} + +int removeChildInRight(TNode *p_tnode) { + TNode *c_tnode = (TNode *)p_tnode->home->tail->value; + c_tnode->father = NULL; + releaseOnlyNode(c_tnode->room); + c_tnode->room = NULL; + p_tnode->child_num--; + popFromTail(p_tnode->home); + return 0; +} + + +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; +} + +int _dogetChildById(const 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; +int _dogetChildByValue(const 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; +} + +int _dogetChildByValue(const 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 -1; +} + +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 (_doTreeThroughUp(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 (_doTreeThroughUp(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; +} + +int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + TNode *p_tnode = p_tree->root; + unsigned long long height = 0; + if (p_tnode != NULL) { + int func_back = func(p_tnode, height); + while (func_back > -2) { + if (func_back > -1) { + p_tnode = getChildByIndex(p_tnode, func_back); + func(p_tnode, height + 1); + } + else + { + p_tnode = p_tnode->father; + func(p_tnode, height - 1); + + } + } + } + return 0; +} +int releaseTNode(TNode *p_tnode) { + if (p_tnode->child_num == 0) { + releaseList(p_tnode->home); + if (p_tnode->father != NULL) { + removeChildById(p_tnode->father, p_tnode->id); + } + if (strcmp(p_tnode->type, "pointer")) { + if (!strcmp(p_tnode->type, "list")) { + releaseList((List *)p_tnode->value); + } + else { + free(p_tnode->value); + } + } + p_tnode->value = NULL; + p_tnode->type = NULL; + p_tnode->id = 0; + p_tnode->if_malloc = 0; + free(p_tnode); + } + return 0; +} + +int releaseTree(Tree *p_tree) { + TreeThroughUp(p_tree, _doreleaseTree); + p_tree->root = NULL; + p_tree->id = 0; + free(p_tree); + return 0; +} + +int _doreleaseTree(TNode *p_tnode, unsigned long long height) { + releaseTNode(p_tnode); + return 0; +} + +int releaseOnlyTree(Tree *p_tree) { + p_tree->id = 0; + p_tree->root = NULL; + free(p_tree); + return 0; +} + +int releaseOnlyTNode(TNode *p_tnode) { + releaseList(p_tnode->home); + if (strcmp(p_tnode->type, "pointer")) { + if (!strcmp(p_tnode->type, "list")) { + releaseList((List *)p_tnode->value); + } + else { + free(p_tnode->value); + } + } + p_tnode->value = NULL; + p_tnode->type = NULL; + p_tnode->id = 0; + p_tnode->if_malloc = 0; + free(p_tnode); + return 0; +} + +int releaseAllForTree(void) { + if (if_safeModeForTree) { + if_safeModeForTree = 0; + Node *p_node = tnode_list->head; + while (p_node != NULL) { + TNode *p_tnode = (TNode *)p_node->value; + releaseOnlyTNode(p_tnode); + } + p_node = tree_list->head; + while (p_node != NULL) { + Tree *p_tree = (Tree *)p_node->value; + releaseOnlyTree(p_tree); + } + releaseList(tnode_list); + releaseList(tree_list); + } + return 0; +} diff --git a/tree/tree.h b/tree/tree.h index 6ceca66..3f5a7ae 100644 --- a/tree/tree.h +++ b/tree/tree.h @@ -1,6 +1,6 @@ -#pragma once - #include "../list/list_expand.h" +#ifndef TREE_H +#define TREE_H typedef struct tree_node { @@ -20,9 +20,9 @@ typedef struct tree TNode *root; }Tree; -List *tree_list = NULL; -List *tnode_list = NULL; -int if_safeModeForTree = 0; +List *tree_list; +List *tnode_list; +int if_safeModeForTree; int safeModeForTree(int ifon); int releaseAllForTree(void); @@ -47,445 +47,23 @@ int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height) int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); +int _dogetChildById(const char *type, void *value); +int _dogetChildByValue(const char *type, void *value); +int _doreleaseTree(TNode *p_tnode, unsigned long long height); +int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); +int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); + int releaseTree(Tree *p_tree); int releaseOnlyTree(Tree *p_tree); int releaseTNode(TNode *p_tnode); -int releaseOnlyTNode(Tree *p_tree); +int releaseOnlyTNode(TNode *p_tnode); -int safeModeForTree(int ifon) { - if (ifon == 1) { - if (tnode_list == NULL && tree_list == NULL) { - tnode_list = (List *)malloc(sizeof(List)); - tree_list = (List *)malloc(sizeof(List)); +char *target_type; +void *target_value; +TNode *target_value_value; - tree_list->head = NULL; - tree_list->length = 0; - tree_list->tail = NULL; - - tnode_list->head = NULL; - tnode_list->length = 0; - tnode_list->tail = NULL; - - if_safeModeForTree = 1; - } - else { - return -1; - } - } - - return ifon; -} - -TNode *initTNode(void) { - TNode *p_tnode = (TNode *)malloc(sizeof(TNode)); - p_tnode->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; - if (if_safeModeForTree) { - if (if_safeModeForNode) { - if_safeModeForNode = 0; - Node *s_node = initNode(); - initMallocValueForTNode(s_node, "pointer", (void *)p_tnode); - insertInTail(tnode_list, s_node); - if_safeModeForNode = 1; - } - else - { - Node *s_node = initNode(); - initMallocValueForTNode(s_node, "pointer", (void *)p_tnode); - insertInTail(tnode_list, s_node); - } - } - return p_tnode; -} - -Tree *initTree(void) { - Tree *p_tree = (Tree *)malloc(sizeof(Tree)); - p_tree->id = getId(); - p_tree->root = NULL; - if (if_safeModeForTree) { - if (if_safeModeForNode) { - if_safeModeForNode = 0; - Node *s_node = initNode(); - initMallocValueForTNode(s_node, "pointer", (void *)p_tree); - if_safeModeForNode = 1; - insertInTail(tree_list, s_node); - } - else - { - Node *s_node = initNode(); - initMallocValueForTNode(s_node, "pointer", (void *)p_tree); - insertInTail(tree_list, s_node); - } - } - 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; -} - -int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { - TNode *p_tnode = p_tree->root; - unsigned long long height = 0; - if (p_tnode != NULL) { - int func_back = func(p_tnode, height); - while (func_back > -2) { - if (func_back > -1) { - p_tnode = getChildByIndex(func_back,func_back); - func(p_tnode, height + 1); - } - else - { - p_tnode = p_tnode->father; - func(p_tnode, height - 1); - - } - } - } - return 0; -} -int releaseTNode(TNode *p_tnode) { - if(p_tnode->child_num == 0){ - releaseList(p_tnode->home); - if (p_tnode->father != NULL) { - removeChildById(p_tnode->father, p_tnode->id); - } - if (strcmp(p_tnode->type, "pointer")) { - if (!strcmp(p_tnode->type, "list")) { - releaseList((List *)p_tnode->value); - } - else { - free(p_tnode->value); - } - } - p_tnode->value = NULL; - p_tnode->type = NULL; - p_tnode->id = 0; - p_tnode->if_malloc = 0; - free(p_tnode); - } - return 0; -} - -int _doreleaseTree(TNode *p_tnode, int height); - -int releaseTree(Tree *p_tree) { - TreeThroughUp(p_tree, _doreleaseTree); - p_tree->root = NULL; - p_tree->id = 0; - free(p_tree); -} - -int _doreleaseTree(TNode *p_tnode, int height) { - releaseTNode(p_tnode); - return 0; -} +unsigned long long target_id; +TNode *target_value_id; -int releaseOnlyTree(Tree *p_tree) { - p_tree->id = 0; - p_tree->root = NULL; - free(p_tree); - return 0; -} - -int releaseOnlyTNode(TNode *p_tnode) { - releaseList(p_tnode->home); - if (strcmp(p_tnode->type, "pointer")) { - if (!strcmp(p_tnode->type, "list")) { - releaseList((List *)p_tnode->value); - } - else { - free(p_tnode->value); - } - } - p_tnode->value = NULL; - p_tnode->type = NULL; - p_tnode->id = 0; - p_tnode->if_malloc = 0; - free(p_tnode); - return 0; -} - -int releaseAllForTree(void) { - if (if_safeModeForTree) { - if_safeModeForTree = 0; - Node *p_node = tnode_list->head; - while (p_node != NULL) { - TNode *p_tnode = (TNode *)p_node->value; - releaseOnlyTNode(p_tnode); - } - Node *p_node = tree_list->head; - while (p_node != NULL) { - Tree *p_tree = (Tree *)p_node->value; - releaseOnlyTree(p_tree); - } - releaseList(tnode_list); - releaseList(tree_list); - } - return 0; -} \ No newline at end of file +#endif \ No newline at end of file