home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / macabuse / src / net / mac / isllist1.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  1.5 KB  |  89 lines

  1. #ifndef ISLLIST_HH
  2. #define ISLLIST_HH
  3.  
  4. template <class T>
  5. class isllist
  6. {
  7. protected:
  8.     class list_node;
  9.     typedef list_node *link;
  10.     class list_node
  11.     {
  12.     public:
  13.         link next;
  14.         T data;
  15.         
  16.         list_node() {}
  17.         list_node(const T& item) { data = item; }
  18.     };
  19.     
  20.     link list;
  21.     link end;
  22. public:
  23.     class iterator
  24.     {
  25.     friend class isllist;
  26.     protected:
  27.         link node;
  28.         iterator(const link p) : node(p) {}
  29.     public:
  30.         iterator() {}
  31.         iterator(const iterator &p) : node(p.node) {}
  32.  
  33.         int operator==(const iterator &p) const {    return (node == p.node); }
  34.         int operator!=(const iterator &p) const { return (node != p.node); }
  35.         
  36.         iterator& operator++() { node = node->next; return *this;    }
  37.         
  38.         T& operator*() { return node->data; }
  39.     };
  40.     
  41.     iterator end()      { return (link)(&list); }
  42.     iterator begin() { return list; }
  43.     
  44.     int empty() { return begin() == end(); }
  45.     
  46.     iterator insert_next(iterator pos, const T& item)
  47.     {
  48.         link p = new list_node(item);
  49.         p->next = pos.node->next;
  50.         pos.node->next = p;
  51.         
  52.         return p; 
  53.     }
  54.     
  55.     void erase_next(iterator pos)
  56.     {
  57.         link p = pos.node->next;
  58.         pos.node->next = p->next;
  59.         delete p;
  60.     }
  61.     
  62.     int find(iterator& p, T& item)
  63.     {
  64.         while (p != end())
  65.         {
  66.             if (*p==item)
  67.                 return 1;
  68.             ++p;
  69.         }
  70.         return 0;
  71.     }
  72.     
  73.     int find(T& item) { iterator p; return find(p, item); }
  74.     void insert(const T& item) {    insert_next(end(), item); }
  75.     void erase() { erase_next( end() ); }
  76.  
  77.     void erase_all()
  78.     {
  79.         while (!empty())
  80.             erase();
  81.     }
  82.     
  83.     isllist()
  84.     {
  85.         list = (link)&list;
  86.     }
  87. };
  88.  
  89. #endif