home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX10012.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  2.5 KB  |  105 lines

  1. // \EXAMPLES\EX10012.CPP
  2. //--------------------------------------------------------------
  3. // member functions of the PhoneNum, List and ListIter classes
  4. //--------------------------------------------------------------
  5.  
  6. //  files in this example:
  7. // %F,15,EX10011.H%EX10011.H       definition of the class String
  8. // %F,15,EX10012.H%EX10012.H       definition of the classes
  9. //                                 PhoneNum, List, and ListIter
  10. // %F,15,EX10013.H%EX10013.H       definition of the class PhoneBk
  11. // %F,15,EX10011.CPP%EX10011.CPP   member functions of the class String
  12. // EX10012.CPP   this file
  13. // %F,15,EX10013.CPP%EX10013.CPP   member functions of the class PhoneBk
  14. // %F,15,EX1001.CPP%EX1001.CPP      main() to exercise this class
  15.  
  16.  
  17. #include "EX10012.H"
  18. #include <iostream.h>
  19.  
  20.  
  21. //--------------------------------------------------------------
  22. // member funtions of the PhoneNum class
  23. //--------------------------------------------------------------
  24.  
  25. ostream& PhoneNum::print(ostream& os)
  26. { name.print(os)
  27.      << "\t"
  28.      << tel
  29.      << "\t"
  30.      << endl;
  31.    return os;
  32. }
  33.  
  34.  
  35. //--------------------------------------------------------------
  36. // member functions of the List class
  37. //--------------------------------------------------------------
  38.  
  39.  
  40. List::~List ()
  41. { ListIter next(*this);
  42.   PhoneNum* entry;
  43.   while ( ( entry = next() ) != NULL )
  44.   {  cout << "destroying:  ";
  45.      entry-> print(cout);
  46.      delete entry;  }
  47. }
  48.  
  49. PhoneNum* List::insert(char* who, long telnum, ListIter next )
  50. {
  51.   PhoneNum* entry = new PhoneNum( who, telnum);
  52.   if ( entry == NULL ) return entry;
  53.   PhoneNum* prior = next.prev(entry);
  54.   if ( prior == NULL )
  55.   {
  56.     entry->link = first;
  57.     first = entry;
  58.   }
  59.   else
  60.   { entry->link = prior->link;
  61.     prior->link = entry;
  62.   }
  63.   return entry;
  64. }
  65.  
  66. void List::del(PhoneNum* entry, ListIter next)
  67. {
  68.   if ( entry == NULL )
  69.     return;
  70.   PhoneNum* prior = next.prev(entry->link);
  71.   if (entry == first )
  72.   { first = entry->link;
  73.     delete entry;
  74.   }
  75.   else
  76.   { prior->link = entry->link;
  77.     delete entry;
  78.   }
  79. }
  80.  
  81.  
  82. //--------------------------------------------------------------
  83. // member functions of the ListIter class
  84. //--------------------------------------------------------------
  85.  
  86.  
  87. PhoneNum* ListIter::operator() ()
  88. {
  89.   if( current == NULL )
  90.   { prior = NULL;
  91.     current = list->first;
  92.   }
  93.   else
  94.   { prior = current;
  95.     current = current->link;
  96.   }
  97.   return current;
  98. }
  99.  
  100. PhoneNum* ListIter::prev( PhoneNum* entry)
  101. { current = entry;
  102.   return prior;
  103. }
  104.  
  105.