修正。

This commit is contained in:
Saturneric 2018-06-12 19:07:12 +08:00
parent 71eb84f190
commit 6dbd74f199
13 changed files with 911 additions and 455 deletions

Binary file not shown.

Binary file not shown.

328
list/list_expand.c Normal file
View File

@ -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);
}

Binary file not shown.

12
main.c
View File

@ -1,12 +0,0 @@
#include<stdio.h>
int stack(void);
int list(void);
int tree(void);
int main(int argc, char **argv) {
list();
tree();
getchar();
return 0;
}

Binary file not shown.

Binary file not shown.

51
stack/stack_expand.c Normal file
View File

@ -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;
}

Binary file not shown.

63
test.c Normal file
View File

@ -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;
}

15
test.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef TEST_H
#define TEST_H
#include<stdio.h>
#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

View File

@ -1,5 +1,438 @@
#include"tree.h" #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; return 0;
} }
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;
}

View File

@ -1,6 +1,6 @@
#pragma once
#include "../list/list_expand.h" #include "../list/list_expand.h"
#ifndef TREE_H
#define TREE_H
typedef struct tree_node typedef struct tree_node
{ {
@ -20,9 +20,9 @@ typedef struct tree
TNode *root; TNode *root;
}Tree; }Tree;
List *tree_list = NULL; List *tree_list;
List *tnode_list = NULL; List *tnode_list;
int if_safeModeForTree = 0; int if_safeModeForTree;
int safeModeForTree(int ifon); int safeModeForTree(int ifon);
int releaseAllForTree(void); 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 TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height));
int TreeTravel(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 releaseTree(Tree *p_tree);
int releaseOnlyTree(Tree *p_tree); int releaseOnlyTree(Tree *p_tree);
int releaseTNode(TNode *p_tnode); int releaseTNode(TNode *p_tnode);
int releaseOnlyTNode(Tree *p_tree); int releaseOnlyTNode(TNode *p_tnode);
int safeModeForTree(int ifon) { char *target_type;
if (ifon == 1) { void *target_value;
if (tnode_list == NULL && tree_list == NULL) { TNode *target_value_value;
tnode_list = (List *)malloc(sizeof(List));
tree_list = (List *)malloc(sizeof(List));
tree_list->head = NULL; unsigned long long target_id;
tree_list->length = 0; TNode *target_value_id;
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;
}
int releaseOnlyTree(Tree *p_tree) { #endif
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;
}