home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctdemo.zip / classes / @CLSLIB1.ZIP / cls / tutorial / slst_bas.cpp < prev    next >
C/C++ Source or Header  |  1995-03-30  |  2KB  |  145 lines

  1. #ifndef INC_SLIST_BASE
  2. #define INC_SLIST_BASE
  3.  
  4. #ifndef __FIRST__
  5. #define __FIRST__
  6. #define __IMPL__SLIST_BASE
  7. #endif
  8.  
  9. /////V slist_base PCM f:\cls_ibm\cls\tutorial 3  PM 30.03.95 10:09:14
  10. /*
  11. /////H
  12. 20.10.94 19:37 PM 0 copied from: slist_base TOS f:\cls_ibm\cls\tutorial 0 PM 19.10.94 00:26:00
  13. 30.03.95 10:34 PM 3 archived: slist_base PCM f:\cls_ibm\cls\tutorial 3  PM 30.03.95 10:09:14
  14. /////
  15. */
  16.  
  17. /////1
  18. #undef inline
  19.  
  20. #include <bsa.h>
  21.  
  22. /////I stdio.h @ @ @ @ pre 
  23. #include <stdio.h>
  24.  
  25. /////T aux pre 
  26. struct slink {
  27.     slink*        next;
  28.  
  29.     slink ()            { next = 0; }
  30.     slink ( slink* p )    { next = p; }
  31. };
  32. /////
  33.  
  34.  
  35. #ifndef __INLINE__
  36. #define inline
  37. #endif
  38.  
  39. /////C slist_base @ @ level0 
  40. class slist_base
  41.  
  42. {
  43. /////N slist_base_iter @
  44. friend class slist_base_iter;
  45.  
  46.  
  47. private:
  48. /////D last @  @ instance private 
  49.     slink*  last;
  50.  
  51.  
  52. public:
  53.     void   append (slink*);
  54.     inline void   clear ();
  55.     slink*  get ();
  56.     void   insert (slink*);
  57.     inline slist_base (slink* =0);
  58. };
  59.  
  60.  
  61. /////2
  62. #undef inline
  63.  
  64.  
  65. #if (defined __INLINE__) || (defined __IMPL__SLIST_BASE)
  66.  
  67. #ifndef __INLINE__
  68. #define inline
  69. #endif
  70.  
  71. /////F clear @ @ public instance inline 
  72. inline
  73. void slist_base:: clear ()
  74.     // clean up and reset list
  75. {
  76.     /*
  77.      * should free the slink chain in order to clean up propperly
  78.      * (otherwise we have a nice memory leak)
  79.      */
  80.  
  81.     last = 0;
  82. }
  83.  
  84. /////F slist_base @ 0 @ public instance inline 
  85. inline
  86. slist_base:: slist_base ( slink* a )
  87.     // default ctor for empty and one-element-initialized lists
  88. {
  89.     if ( a )
  90.         last = a->next = a;
  91.     else
  92.         last = 0;
  93. }
  94.  
  95. /////
  96. #endif
  97.  
  98. /////3
  99. #undef inline
  100.  
  101. #ifdef __IMPL__SLIST_BASE
  102. /////F append @ @ instance public 
  103. void slist_base:: append ( slink* a )
  104.     // add new element at tail of list
  105. {
  106.     if ( last ) {
  107.         a->next = last->next;
  108.         last = last->next = a;
  109.     }
  110.     else
  111.         last = a->next = a;
  112. }
  113.  
  114. /////F get @ @ instance public 
  115. slink* slist_base:: get ()
  116.     // return and remove head of list
  117. {
  118.     if ( last == 0 )
  119.         puts( "get from empty list");        // will be replaced by RAISE (XH)
  120.  
  121.     slink* f = last->next;
  122.     if ( f == last )
  123.         last = 0;
  124.     else
  125.         last->next = f->next;
  126.  
  127.     return f;
  128. }
  129.  
  130. /////F insert @ @ instance public 
  131. void slist_base:: insert ( slink* a )
  132.     // insert new element at head of list
  133. {
  134.     if ( last )
  135.         a->next = last->next;
  136.     else
  137.         last = a;
  138.     last->next = a;
  139. }
  140.  
  141. /////
  142. #endif
  143.  
  144. #endif
  145.