comparison baum.c @ 5:c202ccccedb5

added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
author meillo@marmaro.de
date Fri, 08 Feb 2008 20:45:17 +0100
parents 24a697f37e7c
children ab87b154a96b
comparison
equal deleted inserted replaced
4:24a697f37e7c 5:c202ccccedb5
13 13
14 #include "baum.h" 14 #include "baum.h"
15 #include "actions.h" 15 #include "actions.h"
16 16
17 17
18 struct Node* root; 18 struct Node* root = 0;
19 19
20 20
21 void logit(char* text) { 21 void logit(char* text) {
22 fprintf(stderr, "[%s]\n", text); 22 fprintf(stderr, "[%s]\n", text);
23 } 23 }
56 node->right = insert; 56 node->right = insert;
57 } 57 }
58 58
59 /* delete */ 59 /* delete */
60 void delete(struct Node* node) { 60 void delete(struct Node* node) {
61 if (node->down != NULL) { 61 if (node != NULL) {
62 delete(node->down); 62 if (node->down != NULL) {
63 delete(node->down);
64 }
65 if (node->right != NULL) {
66 delete(node->right);
67 }
68 free(node); node=0;
63 } 69 }
64 if (node->right != NULL) {
65 delete(node->right);
66 }
67 free(node); node=0;
68 } 70 }
69 71
70 72
71 /* print */ 73 /* print */
72 void printNode(struct Node* node) { 74 void printNode(struct Node* node) {
73 printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value); 75 if (node != NULL) {
76 fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
77 }
74 } 78 }
75 79
76 void printTree(struct Node* root) { 80 void printTree(struct Node* root) {
77 printNode(root); 81 if (root != NULL) {
78 printf(" down: "); 82 printNode(root);
79 if (root->down != NULL) { 83 fprintf(stderr, " down: ");
80 printTree(root->down); 84 if (root->down != NULL) {
81 } else { 85 printTree(root->down);
82 printf("NULL\n"); 86 } else {
83 } 87 fprintf(stderr, "NULL\n");
84 printf(" right: "); 88 }
85 if (root->right != NULL) { 89 fprintf(stderr, " right: ");
86 printTree(root->right); 90 if (root->right != NULL) {
87 } else { 91 printTree(root->right);
88 printf("NULL\n"); 92 } else {
93 fprintf(stderr, "NULL\n");
94 }
89 } 95 }
90 } 96 }
91 97
92 98
93 99
98 } 104 }
99 105
100 /* init */ 106 /* init */
101 void init() { 107 void init() {
102 /* add some numbers 108 /* add some numbers
103 root = newNode("print", 0); 109 root = newNode("print", 'n');
104 root->down = newNode("sum", 0); 110 root->down = newNode("sum", 0);
105 111
106 root->down->down = newNode("number", 60); 112 root->down->down = newNode("number", 60);
107 113
108 root->down->down->right = newNode("number", 50); 114 root->down->down->right = newNode("number", 50);
110 root->down->down->right->right = newNode("sum", 0); 116 root->down->down->right->right = newNode("sum", 0);
111 root->down->down->right->right->down = newNode("number", 1); 117 root->down->down->right->right->down = newNode("number", 1);
112 root->down->down->right->right->down->right = newNode("number", 5); 118 root->down->down->right->right->down->right = newNode("number", 5);
113 */ 119 */
114 120
115 /* input numbers and add them */ 121 /* input numbers and add them
116 root = newNode("print", 0); 122 root = newNode("print", 'n');
117 root->down = newNode("sum", 0); 123 root->down = newNode("sum", 0);
118 root->down->down = newNode("input", 0); 124 root->down->down = newNode("input", 0);
119 root->down->down->right = newNode("input", 0); 125 root->down->down->right = newNode("input", 0);
120 root->down->down->right->right = newNode("input", 0); 126 root->down->down->right->right = newNode("input", 0);
127 */
128
129 /* prints HelloWorld
130 */
131 struct Node* np = 0;
132
133 root = newNode("blackhole", 0);
134 root->down = newNode("sum", 0);
135 root->down->down = newNode("print", 'c');
136 np = root->down->down;
137 np->down = newNode("number", 'H');
138
139 np->right = newNode("print", 'c');
140 np->right->down = newNode("number", 'e');
141 np = np->right;
142
143 np->right = newNode("print", 'c');
144 np->right->down = newNode("number", 'l');
145 np = np->right;
146
147 np->right = newNode("print", 'c');
148 np->right->down = newNode("number", 'l');
149 np = np->right;
150
151 np->right = newNode("print", 'c');
152 np->right->down = newNode("number", 'o');
153 np = np->right;
154
155 np->right = newNode("print", 'c');
156 np->right->down = newNode("number", ' ');
157 np = np->right;
158
159 np->right = newNode("print", 'c');
160 np->right->down = newNode("number", 'L');
161 np = np->right;
162
163 np->right = newNode("print", 'c');
164 np->right->down = newNode("number", 'y');
165 np = np->right;
166
167 np->right = newNode("print", 'c');
168 np->right->down = newNode("number", 'd');
169 np = np->right;
170
171 np->right = newNode("print", 'c');
172 np->right->down = newNode("number", 'i');
173 np = np->right;
174
175 np->right = newNode("print", 'c');
176 np->right->down = newNode("number", 10);
177 np = np->right;
178
179
121 } 180 }
122 181
123 182
124 /* main */ 183 /* main */
125 int main(int argc, char* argv[]) { 184 int main(int argc, char* argv[]) {
185 unsigned char shell_return;
126 init(); 186 init();
127 187
128 printTree(root); 188 printTree(root);
129 printf("\n\n"); 189 fprintf(stderr, "\n\n");
130 190
131 action(root); 191 shell_return = action(root);
132 192
133 printf("\n\n"); 193 fprintf(stderr, "\n\n");
134 printTree(root); 194 printTree(root);
135 195
136 delete(root); 196 delete(root);
137 197
138 return(0); 198 exit(shell_return);
139 } 199 }