home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_09_07
/
9n07116c
< prev
next >
Wrap
Text File
|
1991-05-09
|
1KB
|
80 lines
/*
* xrt.cpp - cross-reference table
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ln_seq.h"
#include "xrt.h"
struct treenode
{
char *word;
ln_seq lines;
treenode *left, *right;
treenode(unsigned n);
~treenode();
};
treenode::treenode(unsigned n) : lines(n) { }
treenode::~treenode()
{
delete word;
delete left;
delete right;
}
static treenode *addtree
(treenode *t, char *w, unsigned n)
{
int cond;
if (t == 0)
{
t = new treenode(n);
t->word = new char[strlen(w) + 1];
strcpy(t->word, w);
t->left = t->right = 0;
}
else if ((cond = strcmp(w, t->word)) == 0)
t->lines.add(n);
else if (cond < 0)
t->left = addtree(t->left, w, n);
else
t->right = addtree(t->right, w, n);
return t;
}
static void printtree(treenode *t)
{
if (t != 0)
{
printtree(t->left);
printf("%12s: ", t->word);
t->lines.print();
printf("\n");
printtree(t->right);
}
}
static treenode *root = 0;
void xrt_add(char *w, unsigned n)
{
root = addtree(root, w, n);
}
void xrt_destroy(void)
{
delete root;
}
void xrt_print(void)
{
printtree(root);
}