home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / contain / abslist.cpp next >
Encoding:
C/C++ Source or Header  |  1993-06-21  |  2.0 KB  |  125 lines

  1. #include "abslist.h"
  2.  
  3. link_node::link_node(containable *arg)
  4.  {
  5.   next = NULL;
  6.   data = arg;
  7.  }
  8.  
  9. link_node::~link_node()
  10.  {
  11.   if (data != NULL)
  12.     delete data;
  13.  }
  14.  
  15. abstract_index::abstract_index(abstract_list & base)
  16.  {
  17.   attach(base);
  18.  }
  19.  
  20. void abstract_index::attach(abstract_list &base)
  21.  {
  22.   list = &base;
  23.   rewind();
  24.  }
  25.  
  26. void abstract_index::rewind()
  27.  {
  28.   if (list == NULL)
  29.     data = NULL;
  30.    else
  31.     data = list->head;
  32.  }
  33.  
  34. int abstract_index::has(containable *arg, int exact)
  35.  {
  36.   rewind();
  37.   if (exact) {
  38.     while (data != NULL) {
  39.       if (data->item() == arg) return 1;
  40.       (*this)++;
  41.      }
  42.    } else {
  43.     while (data != NULL) {
  44.       if (*data->item() == *arg) return 1;
  45.       (*this)++;
  46.      }
  47.    }
  48.   return 0;
  49.  }
  50.  
  51. void abstract_index::put(link_node *arg)
  52.  {
  53.   list->object_count++;
  54.   if (data == NULL)
  55.     list->head = arg;
  56.    else {
  57.     arg->next = data->next;
  58.     data->next = arg;
  59.    }
  60.   data = arg;
  61.  }
  62.  
  63. containable *abstract_index::remove()
  64.  {
  65.   containable *result;
  66.  
  67.   if (data == NULL) return NULL;
  68.   result = item();     // preserve value for this index
  69.   data->empty();
  70.   destroy();
  71.   return result;
  72.  }
  73.  
  74. void abstract_index::destroy() {};
  75. void abstract_index::operator -- ( int ) {};
  76.  
  77. abstract_list::abstract_list()
  78.  {
  79.   head = NULL;
  80.  }
  81.  
  82. abstract_list::~abstract_list()
  83.  {
  84.   empty(1);
  85.  }
  86.  
  87. void abstract_list::empty(char kill)
  88.  {
  89.   if (kill) {
  90.     while (object_count)
  91.       destroy();
  92.    } else {
  93.     while (object_count)
  94.       remove();
  95.    }
  96.  }
  97.  
  98. void abstract_list::destroy()
  99.  {
  100.   link_node  *old_node = head;
  101.   if (head == NULL) return;
  102.   head = head->next;
  103.   delete old_node;
  104.   object_count--;
  105.  }
  106.  
  107. containable *abstract_list::remove()
  108.  {
  109.   containable *result;
  110.  
  111.   if (head == NULL) return NULL;
  112.   result = head->item();
  113.   head->empty();
  114.   destroy();
  115.   return result;
  116.  }
  117.  
  118. int abstract_list::has(containable *arg, int exact)
  119.  {
  120.   abstract_index i(*this);
  121.  
  122.   return i.has(arg, exact);
  123.  }
  124.  
  125.