home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / lists.pak / DLIST.CPP next >
C/C++ Source or Header  |  1997-07-23  |  2KB  |  77 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // dlist.cpp
  4. //    Must link with myclass.cpp.
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <classlib/dlistimp.h>
  8. #include <iostream.h>
  9. #include <strstrea.h>
  10. #include "../myclass.h"
  11.  
  12. typedef TDoubleListImp<MyClass> ContainerType;
  13. typedef TDoubleListIteratorImp<MyClass> IteratorType;
  14.  
  15. const int MaxItems=6;
  16.  
  17. void ForEachCallBack(MyClass& mc, void* s)
  18. {
  19.     cout << (char*)s << mc << endl;
  20. }
  21.  
  22. void AddItems(ContainerType& container, int numItems)
  23. {
  24.     for( int i=numItems; i>0; i-- )
  25.         {
  26.         char buf[80];
  27.         ostrstream str( buf, sizeof(buf) );
  28.         str << i << " hello" << ends;
  29.         if (i%2 == 0)
  30.             container.Add(MyClass(buf));    // defaults to adding at head
  31.         else
  32.             container.AddAtTail(MyClass(buf));
  33.         }
  34. }
  35.  
  36. void UseForwardIterator(ContainerType& container)
  37. {
  38.     IteratorType iterator(container);
  39.     while( iterator )
  40.         {
  41.         cout << iterator.Current() << endl;
  42.         iterator++;
  43.         }
  44. }
  45.  
  46. void UseBackwardIterator(ContainerType& container)
  47. {
  48.     IteratorType iterator(container);
  49.     iterator.RestartAtTail();
  50.     while( iterator )
  51.         {
  52.         cout << iterator.Current() << endl;
  53.         iterator--;
  54.         }
  55. }
  56.  
  57. int main()
  58. {
  59.     ContainerType container;
  60.     AddItems(container, MaxItems);
  61.   
  62.     cout << "--- Starting ForEach" << endl;
  63.     container.ForEach(ForEachCallBack, (void*)"FE ");
  64.   
  65.     cout << "--- Starting Iterator (forward)" << endl;
  66.     UseForwardIterator(container);
  67.   
  68.     cout << "--- Deleting '3 hello'" << endl;
  69.     container.Detach(MyClass("3 hello"));  // detach and delete
  70.   
  71.     cout << "--- Starting Iterator (backward)" << endl;
  72.     UseBackwardIterator(container);
  73.   
  74.     return 0;
  75. }
  76.  
  77.