home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Communications / Divers / DinkClass ƒ / DinkClass / DListStuff.c < prev    next >
Encoding:
Text File  |  1992-12-31  |  2.9 KB  |  174 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        DListStuff.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Mark Gross
  7.  
  8.     Copyright:    © 1992 by Applied Technical Software, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>    12/31/92    MTG        making the code conditionaly compiled so         that I am
  13.                                     always working with a current         version in either think c
  14.                                     or MPW C++
  15.          <3>    11/14/92    MTG        Bringing the C++ version up to date WRT the ThinkC version.
  16.          <2>     9/20/92    MTG        Binging the C++ verion of DinkClass up to Date with the THINK C
  17.                                     version
  18.  
  19.     To Do:
  20. */
  21.  
  22.  
  23. // This is the file implementing the three classes
  24. // Tlink, Tlist, and TIterator
  25.  
  26. #include "DListStuff.h"
  27.  
  28.  
  29.     
  30. DLink* DLink::Init(DLink *n, void *item)
  31. {
  32.     if (n)
  33.         fNext = n;
  34.     else
  35.         fNext = NULL;
  36.     
  37.     fItem = item;
  38.     return this;
  39. }// end of constuctor
  40.  
  41.  
  42. DLink*    DLink::GetNext(void)
  43. {
  44.     return    fNext;
  45. }
  46.  
  47. void*    DLink::GetItem(void)
  48. {
  49.     return    fItem;
  50. }
  51.  
  52. void    DLink::SetNext(DLink* aLink)
  53. {
  54.     fNext = aLink;
  55. }
  56.  
  57. void    DLink::SetItem(void* anItem)
  58. {
  59.     fItem = anItem;
  60. }
  61.  
  62. // end of DLink class definition
  63.  
  64.  
  65.     
  66. DList* DList::Init(void)
  67. {
  68.     fLink = NULL;
  69.     fNumItems = 0;
  70.     return this;
  71. }// end of constuctor
  72.  
  73.  
  74. void    DList::AddItem(void* item)
  75. {
  76.     fLink = (new DLink)->Init(fLink, item);
  77.     fNumItems++;
  78. }// end of function AddItem
  79.  
  80.  
  81.  
  82. Boolean    DList::ItemInList(void* item)
  83. {
  84.     DLink  *temp;
  85.         
  86.     for(temp = fLink; temp!=NULL; temp = temp->GetNext() )
  87.     {    if(temp->GetItem() == item)
  88.             return true; //item found
  89.     }
  90.     return false; // item not in list!!!
  91. }
  92.  
  93. Boolean    DList::RemoveItem(void* item)
  94. {
  95.     DLink  *temp, *last;
  96.     
  97.     last = NULL;
  98.     
  99.     for(temp = fLink; temp!=NULL; temp = temp->GetNext() )
  100.     {    
  101.         if(temp->GetItem() == item)
  102.         {
  103.             if(last==NULL) // that happens only if fLink == item
  104.                 fLink = temp->GetNext();
  105.             else
  106.                 last->SetNext(temp->GetNext());
  107.             
  108.             delete temp;
  109.             fNumItems--;
  110.             return true; //item found and removed
  111.         }
  112.         else
  113.             last = temp;
  114.     }
  115.     return false; // item not in list and or not removed!!!
  116. }// end of function RemoveItem
  117.  
  118.  
  119.  
  120. int        DList::NumItems(void)
  121. {
  122.     return fNumItems;
  123. }
  124.  
  125.     
  126.  
  127.  
  128. DIterator::DIterator(void)
  129. {
  130.     fInUse = FALSE;
  131. }
  132.  
  133. DIterator::~DIterator(void)
  134. {
  135.     //Stub
  136. }
  137.  
  138.     
  139. DIterator* DIterator::Init(DList* list)
  140. {
  141.     if(fInUse == TRUE)
  142.         DebugStr("\pMAJOR LOGIC ERROR!! attempting to use an iterator which is in use!!!");
  143.     fInUse = TRUE;
  144.     fCurLink = list->fLink;
  145.     return this;
  146. }// end of constructor
  147.  
  148.  
  149. void*    DIterator::GetCurrentThenIncrement(void)
  150. {
  151.     DLink* link = fCurLink;
  152.     
  153. //
  154. // In a loop using an instansiation of DIterator, if the link->GetNext()
  155. // link gets removed from the list in the loop, then your loop will go south becuse GeCurrentThenIncr\ement
  156. // would not be able to know that what IT THINKS is the current link isn't a valid pointer
  157. // any more....
  158. //
  159.  
  160.     if(fCurLink)
  161.     {
  162.         fCurLink = fCurLink->GetNext();
  163.         return (link->GetItem() );
  164.     }
  165.     else
  166.     {
  167.         fInUse = FALSE;// all done!
  168.         return NULL;
  169.     }
  170.         
  171. }// end of function GetCurrentThenIncrement
  172.  
  173.  
  174.