aboutsummaryrefslogtreecommitdiff
path: root/6.3/tree.c
diff options
context:
space:
mode:
Diffstat (limited to '6.3/tree.c')
-rw-r--r--6.3/tree.c60
1 files changed, 47 insertions, 13 deletions
diff --git a/6.3/tree.c b/6.3/tree.c
index c58da53..a83d294 100644
--- a/6.3/tree.c
+++ b/6.3/tree.c
@@ -6,55 +6,89 @@
#include "tree.h"
struct tnode *talloc(void);
+struct linklist *lalloc(void);
+void lfree(struct linklist *p);
+void addln(struct tnode *p, int linenum);
-struct tnode *addtree(struct tnode *p, char *w, int line)
+struct tnode *addtree(struct tnode *p, char *w, int linenum)
{
int cond;
if (p == NULL) {
p = talloc();
p->word = strdup(w);
- p->lc = 0;
- p->lines[p->lc++] = line;
+ p->lines = lalloc();
+ p->lines->linenum = linenum;
+ p->lines->ptr = NULL;
p->left = p->right = NULL;
} else if (!(cond = strcasecmp(w, p->word))) {
- if (p->lc < MAXLINES)
- p->lines[p->lc++] = line;
- else
- error(EXIT_FAILURE, ENOBUFS, "addtree");
+ addln(p, linenum);
} else if (cond < 0) {
- p->left = addtree(p->left, w, line);
+ p->left = addtree(p->left, w, linenum);
} else if (cond > 0) {
- p->right = addtree(p->right, w, line);
+ p->right = addtree(p->right, w, linenum);
}
return p;
}
+void addln(struct tnode *p, int linenum)
+{
+ struct linklist *temp;
+
+ temp = p->lines;
+ while (temp->ptr != NULL && temp->linenum != linenum)
+ temp = temp->ptr;
+
+ if (temp->linenum != linenum) {
+ temp->ptr = lalloc();
+ temp->ptr->linenum = linenum;
+ temp->ptr->ptr = NULL;
+ }
+}
+
void tprint(struct tnode *p)
{
- int lc;
+ struct linklist *temp;
if (p != NULL) {
tprint(p->left);
- printf("---> %s\n", p->word);
- for (lc = 0; lc < p->lc; ++lc)
- printf((lc == p->lc-1) ? "%d" : "%d, ", p->lines[lc]);
+
+ printf("%10s: ", p->word);
+ for (temp = p->lines; temp != NULL; temp = temp->ptr)
+ printf("%4d", temp->linenum);
+ // printf("%d%s", temp->linenum,
+ // (temp->ptr == NULL) ? "\n" : ", ");
putchar('\n');
tprint(p->right);
}
}
+
void tfree(struct tnode *p)
{
if (p != NULL) {
tfree(p->left);
tfree(p->right);
+ lfree(p->lines);
free(p->word);
free(p);
}
}
+void lfree(struct linklist *p)
+{
+ if (p != NULL) {
+ lfree(p->ptr);
+ free(p);
+ }
+}
+
struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
}
+
+struct linklist *lalloc(void)
+{
+ return (struct linklist *) malloc(sizeof(struct linklist));
+}