baum
diff baum.c @ 9:c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
author | meillo@marmaro.de |
---|---|
date | Sat, 09 Feb 2008 16:41:41 +0100 |
parents | 495a56e706dc |
children | 0e15841ae111 |
line diff
1.1 --- a/baum.c Sat Feb 09 12:42:11 2008 +0100 1.2 +++ b/baum.c Sat Feb 09 16:41:41 2008 +0100 1.3 @@ -16,6 +16,7 @@ 1.4 1.5 1.6 struct Node* root = 0; 1.7 +struct Listitem* list = NULL; 1.8 1.9 1.10 void logit(char* text) { 1.11 @@ -27,7 +28,7 @@ 1.12 struct Node* newNode(char* name, unsigned char value) { 1.13 struct Node* node; 1.14 node = (struct Node*) malloc(sizeof(struct Node)); 1.15 - node->name = name; 1.16 + strcpy(node->name, name); 1.17 node->value = value; 1.18 node->right = 0; 1.19 node->down = 0; 1.20 @@ -73,7 +74,7 @@ 1.21 /* print */ 1.22 void printNode(struct Node* node) { 1.23 if (node != NULL) { 1.24 - fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value); 1.25 + fprintf(stderr, "Node: %10s (%d|%c)\n", node->name, node->value, node->value); 1.26 } 1.27 } 1.28 1.29 @@ -103,81 +104,31 @@ 1.30 action(root); 1.31 } 1.32 1.33 -/* init */ 1.34 -void init() { 1.35 - /* add some numbers 1.36 - root = newNode("print", 'n'); 1.37 - root->down = newNode("sum", 0); 1.38 1.39 - root->down->down = newNode("number", 60); 1.40 1.41 - root->down->down->right = newNode("number", 50); 1.42 1.43 - root->down->down->right->right = newNode("sum", 0); 1.44 - root->down->down->right->right->down = newNode("number", 1); 1.45 - root->down->down->right->right->down->right = newNode("number", 5); 1.46 - */ 1.47 +void push(struct Node* node) { 1.48 + struct Listitem* tmp; 1.49 + struct Listitem* new; 1.50 + new = (struct Listitem*) malloc(sizeof(struct Listitem)); 1.51 + new->node = node; 1.52 + tmp = list; 1.53 + list = new; 1.54 + list->next = tmp; 1.55 +} 1.56 +struct Node* pull() { 1.57 + if (list == NULL) { 1.58 + return NULL; 1.59 + } 1.60 + struct Listitem* tmp; 1.61 + struct Node* node; 1.62 + tmp = list; 1.63 + list = list->next; 1.64 + node = tmp->node; 1.65 + free(tmp); tmp=0; 1.66 + return node; 1.67 +} 1.68 1.69 - /* input numbers and add them 1.70 - root = newNode("print", 'n'); 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 - /* prints HelloWorld 1.78 - */ 1.79 - struct Node* np = 0; 1.80 - 1.81 - root = newNode("blackhole", 0); 1.82 - root->down = newNode("sum", 0); 1.83 - root->down->down = newNode("print", 'c'); 1.84 - np = root->down->down; 1.85 - np->down = newNode("number", 'H'); 1.86 - 1.87 - np->right = newNode("print", 'c'); 1.88 - np->right->down = newNode("number", 'e'); 1.89 - np = np->right; 1.90 - 1.91 - np->right = newNode("print", 'c'); 1.92 - np->right->down = newNode("number", 'l'); 1.93 - np = np->right; 1.94 - 1.95 - np->right = newNode("print", 'c'); 1.96 - np->right->down = newNode("number", 'l'); 1.97 - np = np->right; 1.98 - 1.99 - np->right = newNode("print", 'c'); 1.100 - np->right->down = newNode("number", 'o'); 1.101 - np = np->right; 1.102 - 1.103 - np->right = newNode("print", 'c'); 1.104 - np->right->down = newNode("number", ' '); 1.105 - np = np->right; 1.106 - 1.107 - np->right = newNode("print", 'c'); 1.108 - np->right->down = newNode("number", 'L'); 1.109 - np = np->right; 1.110 - 1.111 - np->right = newNode("print", 'c'); 1.112 - np->right->down = newNode("number", 'y'); 1.113 - np = np->right; 1.114 - 1.115 - np->right = newNode("print", 'c'); 1.116 - np->right->down = newNode("number", 'd'); 1.117 - np = np->right; 1.118 - 1.119 - np->right = newNode("print", 'c'); 1.120 - np->right->down = newNode("number", 'i'); 1.121 - np = np->right; 1.122 - 1.123 - np->right = newNode("print", 'c'); 1.124 - np->right->down = newNode("number", 10); 1.125 - np = np->right; 1.126 - 1.127 - 1.128 -} 1.129 1.130 1.131 /* read input */ 1.132 @@ -186,10 +137,16 @@ 1.133 int indent; 1.134 char name[256]; 1.135 int value; 1.136 + int last_indent; 1.137 + struct Node* last_node; 1.138 + struct Node* node; 1.139 1.140 indent = 0; 1.141 strcpy(name, ""); 1.142 value = 0; 1.143 + last_indent = -1; 1.144 + root = newNode("blackhole", 0); 1.145 + last_node = root; 1.146 1.147 while ((c = getchar()) != EOF) { 1.148 if (c == '#') { /* comment */ 1.149 @@ -205,8 +162,21 @@ 1.150 1.151 if (c == '\n') { /* end of line: create node */ 1.152 if (strlen(name) > 0) { 1.153 - printf(" %d - %s - %d\n", indent, name, value); 1.154 + fprintf(stderr, " %d - %s - %d\n", indent, name, value); 1.155 /* create node */ 1.156 + node = newNode((char*) name, value); 1.157 + if (indent > last_indent) { /* down */ 1.158 + last_node->down = node; 1.159 + push(last_node); 1.160 + } else if (indent == last_indent) { /* right */ 1.161 + last_node->right = node; 1.162 + } else if (indent < last_indent) { /* up */ 1.163 + /* FIXME what if it goes more than one level up? */ 1.164 + last_node = pull(); 1.165 + last_node->right = node; 1.166 + } 1.167 + last_indent = indent; 1.168 + last_node = node; 1.169 } 1.170 indent = 0; 1.171 strcpy(name, ""); 1.172 @@ -237,20 +207,14 @@ 1.173 /* main */ 1.174 int main(int argc, char* argv[]) { 1.175 unsigned char shell_return = 0; 1.176 - /* 1.177 - init(); 1.178 - 1.179 + 1.180 + read_input(); 1.181 printTree(root); 1.182 - fprintf(stderr, "\n\n"); 1.183 1.184 shell_return = action(root); 1.185 1.186 - fprintf(stderr, "\n\n"); 1.187 printTree(root); 1.188 + delete(root); 1.189 1.190 - delete(root); 1.191 - */ 1.192 - 1.193 - read_input(); 1.194 exit(shell_return); 1.195 }