添加与修改。

This commit is contained in:
Saturneric 2018-06-13 16:04:51 +08:00
parent 6dbd74f199
commit 42fb87bacd
8 changed files with 260 additions and 11 deletions

Binary file not shown.

View File

@ -120,7 +120,7 @@ void printList(List *p_list) {
printf("%s", (char *)(p_node->value)); printf("%s", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "pointer")) { else if (!strcmp(p_node->type, "pointer")) {
printf("%s", (char *)(p_node->value)); printf("%p", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "list")) { else if (!strcmp(p_node->type, "list")) {
printList((List *)p_node->value); printList((List *)p_node->value);

10
test.c
View File

@ -1,5 +1,6 @@
#include "test.h" #include "test.h"
int list(void) { int list(void) {
init_rand(); init_rand();
safeModeForNode(1); safeModeForNode(1);
@ -39,6 +40,15 @@ int list(void) {
int tree(void) { int tree(void) {
safeModeForTree(1); safeModeForTree(1);
Tree *t_tree = initTree(); Tree *t_tree = initTree();
TNode *t_tnode = tnodeWithInt(1), *cr_tnode = tnodeWithInt(3),*cl_tnode = tnodeWithInt(2);
addChildInRight(t_tnode, cl_tnode);
addChildInRight(t_tnode, cr_tnode);
addChildInRight(cl_tnode, tnodeWithInt(4));
addChildInRight(cl_tnode, tnodeWithInt(5));
addChildInRight(cr_tnode, tnodeWithInt(6));
addChildInRight(cr_tnode, tnodeWithInt(7));
printTNodeWithFamily(t_tnode, 0);
//removeChildByIndex(t_tnode, 0);
releaseAllForTree(); releaseAllForTree();
return 0; return 0;
} }

1
test.h
View File

@ -6,6 +6,7 @@
#include "list/list_expand.h" #include "list/list_expand.h"
#include "stack/stack_expand.h" #include "stack/stack_expand.h"
#include "tree/tree.h" #include "tree/tree.h"
#include "tree/tree_expand.h"
int stack(void); int stack(void);
int list(void); int list(void);

View File

@ -1,5 +1,9 @@
#include"tree.h" #include"tree.h"
unsigned long long target_id = 0;
TNode *target_value_id = NULL;
int if_safeModeForTree = 0;
int safeModeForTree(int ifon) { int safeModeForTree(int ifon) {
if (ifon == 1) { if (ifon == 1) {
if (tnode_list == NULL && tree_list == NULL) { if (tnode_list == NULL && tree_list == NULL) {
@ -86,6 +90,7 @@ int addChildInLeft(TNode *f_tnode, TNode *c_tnode) {
initMallocValueForNode(p_node, "pointer", c_tnode); initMallocValueForNode(p_node, "pointer", c_tnode);
insertInHead(f_tnode->home, p_node); insertInHead(f_tnode->home, p_node);
c_tnode->father = f_tnode; c_tnode->father = f_tnode;
c_tnode->room = p_node;
f_tnode->child_num++; f_tnode->child_num++;
return 0; return 0;
} }
@ -95,6 +100,7 @@ int addChildInRight(TNode *f_tnode, TNode *c_tnode) {
initMallocValueForNode(p_node, "pointer", c_tnode); initMallocValueForNode(p_node, "pointer", c_tnode);
insertInTail(f_tnode->home, p_node); insertInTail(f_tnode->home, p_node);
c_tnode->father = f_tnode; c_tnode->father = f_tnode;
c_tnode->room = p_node;
f_tnode->child_num++; f_tnode->child_num++;
return 0; return 0;
} }
@ -246,7 +252,7 @@ TNode *getChildByIndex(TNode *p_tnode, unsigned long long index) {
int m_index = 0; int m_index = 0;
if (index < p_tnode->child_num - 1) if (index < p_tnode->child_num - 1)
{ {
while (p_node != NULL) { while (p_node != NULL && m_index < index) {
m_index++; m_index++;
p_node = p_node->next; p_node = p_node->next;
} }
@ -402,12 +408,14 @@ int releaseOnlyTree(Tree *p_tree) {
int releaseOnlyTNode(TNode *p_tnode) { int releaseOnlyTNode(TNode *p_tnode) {
releaseList(p_tnode->home); releaseList(p_tnode->home);
if (strcmp(p_tnode->type, "pointer")) { if (p_tnode->if_malloc) {
if (!strcmp(p_tnode->type, "list")) { if (strcmp(p_tnode->type, "pointer")) {
releaseList((List *)p_tnode->value); if (!strcmp(p_tnode->type, "list")) {
} releaseList((List *)p_tnode->value);
else { }
free(p_tnode->value); else {
free(p_tnode->value);
}
} }
} }
p_tnode->value = NULL; p_tnode->value = NULL;
@ -425,14 +433,21 @@ int releaseAllForTree(void) {
while (p_node != NULL) { while (p_node != NULL) {
TNode *p_tnode = (TNode *)p_node->value; TNode *p_tnode = (TNode *)p_node->value;
releaseOnlyTNode(p_tnode); releaseOnlyTNode(p_tnode);
p_node = p_node->next;
} }
p_node = tree_list->head; p_node = tree_list->head;
while (p_node != NULL) { while (p_node != NULL) {
Tree *p_tree = (Tree *)p_node->value; Tree *p_tree = (Tree *)p_node->value;
releaseOnlyTree(p_tree); releaseOnlyTree(p_tree);
p_node = p_node->next;
} }
releaseList(tnode_list); releaseList(tnode_list);
releaseList(tree_list); releaseList(tree_list);
} }
return 0; return 0;
} }
int setRoot(Tree *p_tree, TNode *p_tnode) {
p_tree->root = p_tnode;
return 0;
}

View File

@ -20,8 +20,7 @@ typedef struct tree
TNode *root; TNode *root;
}Tree; }Tree;
List *tree_list;
List *tnode_list;
int if_safeModeForTree; int if_safeModeForTree;
int safeModeForTree(int ifon); int safeModeForTree(int ifon);
int releaseAllForTree(void); int releaseAllForTree(void);
@ -58,6 +57,8 @@ int releaseOnlyTree(Tree *p_tree);
int releaseTNode(TNode *p_tnode); int releaseTNode(TNode *p_tnode);
int releaseOnlyTNode(TNode *p_tnode); int releaseOnlyTNode(TNode *p_tnode);
int setRoot(Tree *p_tree, TNode *p_tnode);
char *target_type; char *target_type;
void *target_value; void *target_value;
TNode *target_value_value; TNode *target_value_value;
@ -65,5 +66,9 @@ TNode *target_value_value;
unsigned long long target_id; unsigned long long target_id;
TNode *target_value_id; TNode *target_value_id;
List *tree_list;
List *tnode_list;
int if_safeModeForTree;
#endif #endif

197
tree/tree_expand.c Normal file
View File

@ -0,0 +1,197 @@
#include "tree_expand.h"
TNode *tnodeWithInt(int temp) {
TNode *p_tnode = initTNode();
int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp;
initMallocValueForTNode(p_tnode, "int", p_temp);
return p_tnode;
}
TNode *tnodeWithDouble(double temp) {
TNode *p_tnode = initTNode();
double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp;
initMallocValueForTNode(p_tnode, "double", p_temp);
return p_tnode;
}
TNode *tnodeWithString(char *temp) {
TNode *p_tnode = initTNode();
char *p_temp = (char *)malloc(sizeof(temp));
strcpy_s(p_temp, sizeof(p_temp), temp);
initMallocValueForTNode(p_tnode, "double", p_temp);
return p_tnode;
}
TNode *tnodeWithPointer(void *temp) {
TNode *p_tnode = initTNode();
initMallocValueForTNode(p_tnode, "pointer", temp);
return p_tnode;
}
int getValueByIntForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "int")) {
return *(int *)p_tnode->value;
}
return -1;
}
double getValueByDoubleForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "double")) {
return *(double *)p_tnode->value;
}
return -1;
}
char *getValueByStringForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "string")) {
return (char *)p_tnode->value;
}
return NULL;
}
void *getValueByPointerForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "pointer")) {
return p_tnode->value;
}
return NULL;
}
int printTNode(TNode *p_tnode, int priority) {
for (int i = 0; i < priority; i++) printf(" ");
if (priority == 0) printf("###");
else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "double")) {
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
}
else printf("NO\n");
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Child number: %llu\n", p_tnode->child_num);
}
return 0;
}
int printTNodeWithHome(TNode *p_tnode,int priority) {
if (priority == 0) printf("###");
else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "double")) {
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
}
else printf("NO\n");
if (p_tnode->father != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father id: %llu\n", p_tnode->father->id);
}
else
{
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father: NO\n");
}
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Child(number: %llu):\n", p_tnode->child_num);
}
List *p_home = p_tnode->home;
Node *p_node = p_home->head;
while (p_node != NULL) {
printTNode((TNode *)p_node->value,priority+2);
p_node = p_node->next;
}
return 0;
}
int printTNodeWithFamily(TNode *p_tnode, int priority) {
for (int i = 0; i < priority ; i++) printf(" ");
if (priority == 0) printf("###");
else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "double")) {
printf("%a\n", *(double *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "string")) {
printf("%s\n", (char *)(p_tnode->value));
}
else if (!strcmp(p_tnode->type, "pointer")) {
printf("%p\n", (char *)(p_tnode->value));
}
}
else printf("NO\n");
if (p_tnode->father != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father id: %llu\n", p_tnode->father->id);
}
else
{
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Father: NO\n");
}
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("Child(number: %llu):\n", p_tnode->child_num);
}
List *p_home = p_tnode->home;
Node *p_node = p_home->head;
while (p_node != NULL) {
printTNodeWithFamily((TNode *)p_node->value, priority + 2);
p_node = p_node->next;
}
return 0;
}
int printTree(Tree *p_tree) {
printf("###");
printf("Tree(id: %llu)",p_tree->id);
printTNodeWithFamily(p_tree->root,0);
return 0;
}

21
tree/tree_expand.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TREE_EXPAND_H
#define TREE_EXPAND_H
#include "tree.h"
TNode *tnodeWithInt(int);
TNode *tnodeWithDouble(double);
TNode *tnodeWithString(char *);
TNode *tnodeWithPointer(void *);
int getValueByIntForTree(TNode *);
double getValueByDoubleForTree(TNode *);
char *getValueByStringForTree(TNode *);
void *getValueByPointerForTree(TNode *);
int printTree(Tree *p_tree);
int printTNodeWithHome(TNode *p_tnode, int priority);
int printTNodeWithFamily(TNode *p_tnode, int priority);
int printTNode(TNode *p_tnode, int priority);
#endif