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

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