baum
diff baum.c @ 3:15d7d6b9766f
added input; added nextNode, lastNode, insertLast
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 16:15:07 +0100 |
parents | 557fa4df2bcd |
children | 24a697f37e7c |
line diff
1.1 --- a/baum.c Thu Feb 07 14:46:27 2008 +0100 1.2 +++ b/baum.c Thu Feb 07 16:15:07 2008 +0100 1.3 @@ -24,17 +24,38 @@ 1.4 1.5 1.6 /* new */ 1.7 -struct Node* newNode(char* name) { 1.8 +struct Node* newNode(char* name, unsigned char value) { 1.9 struct Node* node; 1.10 node = (struct Node*) malloc(sizeof(struct Node)); 1.11 node->name = name; 1.12 - node->value = 0; 1.13 + node->value = value; 1.14 node->right = 0; 1.15 node->down = 0; 1.16 return node; 1.17 } 1.18 1.19 1.20 +void setValue(struct Node* node, unsigned char value) { 1.21 + node->value = value; 1.22 +} 1.23 + 1.24 + 1.25 +struct Node* nextNode(struct Node* node) { 1.26 + return node->right; 1.27 +} 1.28 + 1.29 +struct Node* lastNode(struct Node* node) { 1.30 + while (node->right != NULL) { 1.31 + node = node->right; 1.32 + } 1.33 + return node; 1.34 +} 1.35 + 1.36 +void insertLast(struct Node* node, struct Node* insert) { 1.37 + node = lastNode(node); 1.38 + node->right = insert; 1.39 +} 1.40 + 1.41 /* delete */ 1.42 void delete(struct Node* node) { 1.43 if (node->down != NULL) { 1.44 @@ -78,14 +99,25 @@ 1.45 1.46 /* init */ 1.47 void init() { 1.48 - root = newNode("printchar"); 1.49 - root->down = newNode("number"); 1.50 - root->down = newNode("sum"); 1.51 - root->down->down = newNode("number"); 1.52 - root->down->down->value = 70; /* 'F' */ 1.53 - root->down->down->right = newNode("number"); 1.54 - root->down->down->right->value = 50; /* '2' */ 1.55 - /* result should be 'x' */ 1.56 + /* add some numbers 1.57 + root = newNode("print", 0); 1.58 + root->down = newNode("sum", 0); 1.59 + 1.60 + root->down->down = newNode("number", 60); 1.61 + 1.62 + root->down->down->right = newNode("number", 50); 1.63 + 1.64 + root->down->down->right->right = newNode("sum", 0); 1.65 + root->down->down->right->right->down = newNode("number", 1); 1.66 + root->down->down->right->right->down->right = newNode("number", 5); 1.67 + */ 1.68 + 1.69 + /* input numbers and add them */ 1.70 + root = newNode("print", 0); 1.71 + root->down = newNode("sum", 0); 1.72 + root->down->down = newNode("input", 0); 1.73 + root->down->down->right = newNode("input", 0); 1.74 + root->down->down->right->right = newNode("input", 0); 1.75 } 1.76 1.77 1.78 @@ -96,6 +128,7 @@ 1.79 1.80 action(root); 1.81 1.82 + printTree(root); 1.83 delete(root); 1.84 1.85 return(0);