home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07124a < prev    next >
Text File  |  1991-05-09  |  1KB  |  65 lines

  1. /*
  2.  * xrt.cpp - cross-reference table
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #include "ln_seq.h"
  9. #include "xrt.h"
  10.  
  11. class treenode
  12.     {
  13. public:
  14.     char *word;
  15.     ln_seq lines;
  16.     xrt left, right;
  17.     treenode(char *w, unsigned n);
  18.     ~treenode();
  19.     };
  20.  
  21. treenode::treenode(char *w, unsigned n) : lines(n)
  22.     {
  23.     word = strcpy(new char[strlen(w) + 1], w);
  24.     }
  25.  
  26. treenode::~treenode()
  27.     {
  28.     delete word;
  29.     }
  30.  
  31. xrt::xrt() : root(0) { }
  32.  
  33. xrt::~xrt()
  34.     {
  35.     delete root;
  36.     }
  37.  
  38. void xrt::add(char *w, unsigned n)
  39.     {
  40.     int cond;
  41.  
  42.     if (root == 0)
  43.         root = new treenode(w, n);
  44.     else if ((cond = strcmp(w, root->word)) == 0)
  45.         root->lines.add(n);
  46.     else if (cond < 0)
  47.         root->left.add(w, n);
  48.     else
  49.         root->right.add(w, n);
  50.     }
  51.  
  52. void xrt::print()
  53.     {
  54.     if (root != 0)
  55.         {
  56.         root->left.print();
  57.         printf("%12s: ", root->word);
  58.         root->lines.print();
  59.         printf("\n");
  60.         root->right.print();
  61.         }
  62.     }
  63.  
  64.  
  65.