home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LIST.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  158 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /////////////////////////////////////////////////////////////
  4. // MODULE
  5. //  list.cpp
  6. // CREATED
  7. //  davidn  03 Dec 1994  23:59
  8. //  David L. Nugent
  9. //  This class implementation is donated to the public domain
  10. // DESCRIPTION
  11. //  Implementation of class node, list & iter
  12. // FUNCTIONS
  13. //  node::unlink()
  14. //    Destroys linkage from a list and removes it from a
  15. //    linked list
  16. //  node::link()
  17. //    Links a node into a linked list
  18. //  list::purge()
  19. //    Removes all linked list entries
  20. //  iter::traverse()
  21. //    Provides full traversal functions for nodes in a
  22. //    linked list
  23. /////////////////////////////////////////////////////////////
  24.  
  25. // Implementation of class list & friends
  26.  
  27. #include "list.hpp"
  28.  
  29. void
  30. node::unlink()
  31. {
  32.   if ( mylist )
  33.   {
  34.       // Unlink from previous
  35.     if ( Prev )
  36.       Prev->Next = Next;
  37.     else
  38.       mylist->First = Next;
  39.  
  40.       // Unlink from next
  41.     if ( Next )
  42.       Next->Prev = Prev;
  43.     else
  44.       mylist->Last = Prev;
  45.  
  46.     mylist->nodes--;
  47.     mylist = 0;
  48.     Prev = Next = 0;
  49.   }
  50. }
  51.  
  52. void
  53. node::link( list * L, node * prv, node * nxt )
  54. {
  55.  
  56.     // If currently linked, then unlink it
  57.  
  58.   if ( mylist )
  59.     unlink();
  60.  
  61.     // Link it to the list
  62.  
  63.   if ( L )
  64.   {
  65.  
  66.     mylist = L;
  67.  
  68.     // Add after previous
  69.  
  70.     if ( prv )
  71.     {
  72.  
  73.       Prev = prv;
  74.       Next = prv->Next;
  75.     }
  76.  
  77.     // Add before next
  78.  
  79.     else if ( nxt )
  80.     {
  81.  
  82.       Next = nxt;
  83.       Prev = nxt->Prev;
  84.     }
  85.  
  86.     // Else just add to end
  87.  
  88.     else
  89.     {
  90.  
  91.       Next = 0;
  92.       Prev = L->Last;
  93.     }
  94.  
  95.     if ( Prev )
  96.       Prev->Next = this;
  97.     else
  98.       L->First = this;
  99.  
  100.     if ( Next )
  101.       Next->Prev = this;
  102.     else
  103.       L->Last = this;
  104.  
  105.     mylist->nodes++;
  106.   }
  107. }
  108.  
  109.  
  110. void
  111. list::purge( void )
  112. {
  113.   while ( First )
  114.     delete First;
  115. }
  116.  
  117.  
  118. int
  119. iter::traverse( trOp op )
  120. {
  121.   if ( mylist.firstNode() == 0 )
  122.     return TR_EMPTY;
  123.  
  124.   switch ( op )
  125.   {
  126.  
  127.   case NEXT:
  128.     if ( nptr )
  129.     {
  130.  
  131.       nptr = nptr->Next;
  132.       break;
  133.     }
  134.  
  135.   case FIRST:
  136.     nptr = mylist.firstNode();
  137.     break;
  138.  
  139.  
  140.   case PREV:
  141.     if ( nptr )
  142.     {
  143.       nptr = nptr->Prev;
  144.       break;
  145.     }
  146.  
  147.   case LAST:
  148.     nptr = mylist.lastNode();
  149.     break;
  150.  
  151.   case CURR:
  152.     break;
  153.  
  154.   }
  155.  
  156.   return ( nptr ) ? TR_OK : TR_NOMORE;
  157. }
  158.