home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / PBL30SAM.ZIP / DLSTEST2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-27  |  5.3 KB  |  228 lines

  1. #include "dlist.h"
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define NELEM(a) (sizeof(a)/sizeof(a[0]))
  7.  
  8. struct MyType
  9. {
  10.     int   num;
  11.     int   age;
  12.     char *name;
  13. } array[] = {
  14.     {1,  22, "Branislav"},
  15.     {2,  23, "Duh Man"},
  16.     {3,  46, "Ok, here we go"},
  17.     {4,  10, "Kitty"},
  18.     {5,  50, "Bad attitude"},
  19.     {6,  90, "Now now!"},
  20.     {7,   0, "The intruder"},
  21.     {8,  23, "John Doe"},
  22.     {9,  12, "Charles McCamant"},
  23.     {10, 22, "Twila G. Lehman"},
  24.     {11, 34, "Mark Motl"},
  25.     {12, 45, "K. Elliot"},
  26.     {13, 19, "Charlie Ruster"},
  27.     {14, 49, "Beaches"},
  28.     {15, 72, "Sam Cook"},
  29.     {16, 94, "Sam Cooke"},
  30.     {17, 66, "Ann Margaret"},
  31. };
  32.  
  33. class TMySorted: public zDLSortedList
  34. {
  35. public:
  36.     TMySorted(Boolean shouldPurge = False): zDLSortedList(shouldPurge){}
  37.  
  38. private:
  39.     int  compare(void *key1, void *key2);
  40.     void *keyOf(void *data);
  41. };
  42.  
  43. class TAgeSort: public zDLSortedList
  44. {
  45. public:
  46.     TAgeSort(Boolean shouldPurge = False): zDLSortedList(shouldPurge){}
  47.  
  48. private:
  49.     int compare(void *key1, void *key2){return *(int *)key1 - *(int *)key2;}
  50.     void *keyOf(void *data){ return (void *)&((MyType *)data)->age; }
  51. };
  52.  
  53. void print(void *data, void *dummy = 0);
  54. int  compare(void *data, void *orig);
  55. int  CompareName(void *p1, void *p2);
  56.  
  57.     int
  58. main( void )
  59. {
  60.     zDoubleList list;
  61.     TMySorted   sorted;
  62.     TAgeSort    agesort;
  63.  
  64.     for( int i = 0; i < NELEM(array); ++i ){
  65.         list.link(&array[i]);
  66.         sorted.link(&array[i]);
  67.         agesort.link(&array[i]);
  68.     }
  69.  
  70.     cout << "\n\t>>>>>>>>>> The sorted list:" << endl;
  71.     sorted.forEach(print);
  72.     cout << "\nPress Enter to continue...";
  73.     getchar();
  74.  
  75.     cout << "\n\t>>>>>>>>>> The age sorted list:" << endl;
  76.     agesort.forEach(print);
  77.     cout << "\nPress Enter to continue...";
  78.     getchar();
  79.  
  80.     cout << "\t>>>>>>>>>> List has " << list.size() << " elements.\n" << endl;
  81.     list.forEach(print);
  82.     cout << "\nPress Enter to continue...";
  83.     getchar();
  84.  
  85.     cout << "\n\t>>>>>>>>>> Update 5th element as 2" << endl;
  86.     list.begin();
  87.     list += 4;
  88.     list.update(&array[1]);
  89.     list.forEach(print);
  90.     cout << "\nPress Enter to continue...";
  91.     getchar();
  92.  
  93.     cout << "\n\t>>>>>>>>>> Remove the focus (5th)" << endl;
  94.     list.unlink();
  95.     cout << "\t>>>>>>>>>> List has " << list.size() << " elements now.\n" << endl;
  96.     list.forEach(print);
  97.     cout << "\nPress Enter to continue...";
  98.     getchar();
  99.  
  100.     cout << "\n\t>>>>>>>>>> Prepend 5 and 6" << endl;
  101.     list.end();
  102.     list -= 4;
  103.     list.prepend(&array[5]);
  104.     list.prepend(&array[4]);
  105.     list.forEach(print);
  106.     cout << "\nPress Enter to continue...";
  107.     getchar();
  108.  
  109.     cout << "\n\t>>>>>>>>>> Append 3 and 1" << endl;
  110.     list.append(&array[2]);
  111.     list.append(&array[0]);
  112.     list.forEach(print);
  113.     cout << "\nPress Enter to continue...";
  114.     getchar();
  115.  
  116.     cout << "\n\t>>>>>>>>>> Traversing the list (w/o last element)" << endl;
  117.     for( list.begin(); !list.atEnd(); list.next() ){
  118.         MyType *p = (MyType *)list();
  119.  
  120.         cout << p->name << " "
  121.              << p->age << " "
  122.              << p->num << endl;
  123.     }
  124.     cout << "\nPress Enter to continue...";
  125.     getchar();
  126.  
  127.     cout << "\n\t>>>>>>>>>> Traversing the list backwards (w/o first element)" << endl;
  128.     for( list.end(); !list.atStart(); list.prev() ){
  129.         MyType *p = (MyType *)list();
  130.  
  131.         cout << p->name << " "
  132.              << p->age << " "
  133.              << p->num << endl;
  134.     }
  135.     cout << "\nPress Enter to continue...";
  136.     getchar();
  137.  
  138.     if( list.firstThat(compare, "branislav") ){
  139.         cout << "\n\t>>>>>>>>>> found: " << ((MyType *)list())->name << " "
  140.              << ((MyType *)list())->age << " "
  141.              << ((MyType *)list())->num << endl;
  142.     }
  143.     else cout << "\n\t>>>>>>>>>> not found" << endl;
  144.     cout << "\nPress Enter to continue...";
  145.     getchar();
  146.  
  147.     cout << "\nPrint names only" << endl;
  148.     list.begin();
  149.     do{
  150.         cout << ((MyType *)list())->name << endl;
  151.     }while( list++ );
  152.     cout << "\nPress Enter to continue...";
  153.     getchar();
  154.  
  155.     list.begin(); list += 3;
  156.     zListCursor iter(list, False);
  157.     cout << "\n\t>>>>>>>>>> Construct a cursor at element 4 (list to end)" << endl;
  158.     do{
  159.         cout << ((MyType *)iter())->name << endl;
  160.     }while( iter.next() );
  161.     cout << "\nPress Enter to continue...";
  162.     getchar();
  163.  
  164.     cout << "\n\t>>>>>>>>>> Synchronizing with the list (at beginning)" << endl;
  165.     list.begin();
  166.     iter.sync();
  167.     do{
  168.         cout << ((MyType *)iter())->name << endl;
  169.     }while( iter.next() );
  170.     cout << "\nPress Enter to continue...";
  171.     getchar();
  172.  
  173.     cout << "\n\t>>>>>>>>>> Sorting the list by name" << endl;
  174.     list.sort( CompareName );
  175.     list.begin();
  176.     iter.sync();
  177.     do{
  178.         cout << ((MyType *)iter())->name << endl;
  179.     }while( iter.next() );
  180.     cout << "\nPress Enter to continue...";
  181.     getchar();
  182.  
  183.     cout << "\n\t>>>>>>>>>> Traversing it backwards" << endl;
  184.     iter.end();
  185.     do{
  186.         cout << ((MyType *)iter())->name << endl;
  187.     }while( iter.prev() );
  188.     cout << "\nPress Enter to continue...";
  189.     getchar();
  190.  
  191.     return 0;
  192. }
  193.  
  194.  
  195.     void
  196. print(void *data, void *dummy)
  197. {
  198.     dummy;
  199.     MyType *p = (MyType *)data;
  200.  
  201.     cout << p->num << ". "
  202.          << p->name << " of age "
  203.          << p->age << endl;
  204. }
  205.  
  206. int
  207. compare(void *data, void *arg)
  208. {
  209.     return stricmp(((MyType *)data)->name, (char *)arg) ? False : True;
  210. }
  211.  
  212. int
  213. CompareName(void *p1, void *p2)
  214. {
  215.     return stricmp( ((MyType *)p1)->name, ((MyType *)p2)->name );
  216. }
  217.  
  218. int TMySorted::compare(void *key1, void *key2)
  219. {
  220.     return stricmp((char *)key1, (char *)key2);
  221. }
  222.  
  223. void *TMySorted::keyOf(void *data)
  224. {
  225.     return (void *)((MyType *)data)->name;
  226. }
  227.  
  228.