home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 6 < prev    next >
Encoding:
Text File  |  1992-04-01  |  3.3 KB  |  109 lines

  1. /*
  2.  * Example 3: class GDlist; generic doubly-linked lists, using ints
  3.  *
  4.  * $Header:   E:/vcs/toolexam/example3.cpv   1.1   01 Apr 1992 16:51:10   keffer  $
  5.  *
  6.  ****************************************************************************
  7.  *
  8.  * Rogue Wave Software, Inc.
  9.  * P.O. Box 2328
  10.  * Corvallis, OR 97339
  11.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  12.  *
  13.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  14.  * protection under the laws of the United States and other countries.
  15.  *
  16.  ***************************************************************************
  17.  *
  18.  * $Log:   E:/vcs/toolexam/example3.cpv  $
  19.  * 
  20.  *    Rev 1.1   01 Apr 1992 16:51:10   keffer
  21.  * Now includes <rw/xxx.h>
  22.  * 
  23.  */
  24.  
  25.  
  26. // Include the header file for the class GDlist:
  27. #include <rw/gdlist.h>
  28. #include <rw/rstream.h>
  29.  
  30. /* The class GDlist makes use of the macro declare defined in the header file
  31.  * <generic.h> to implement the current C++ approximation to parameterized types.
  32.  * The first argument is the class name (GDlist).
  33.  * The second argument is the type of object being stored (int).
  34.  */
  35.  
  36. declare(GDlist,int)
  37.  
  38. // Some data to be stored:
  39. int idata[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  40.  
  41. main()
  42. {
  43.   // Construct a linked-list of pointers to ints with no links:
  44.   // The form: GDlist(int) is converted via a macro to intGDlist.
  45.   GDlist(int) L;
  46.  
  47.   cout << "Prepend ints: 4 3 2 1 0\n";
  48.   L.prepend(&idata[4]);
  49.   L.prepend(&idata[3]);
  50.   L.prepend(&idata[2]);
  51.   L.prepend(&idata[1]);
  52.   L.prepend(&idata[0]);
  53.  
  54.   cout << "Insert (append) ints: 5 6 7 8\n";
  55.   L.insert(&idata[5]);
  56.   L.insert(&idata[6]);
  57.   L.insert(&idata[7]);
  58.   L.insert(&idata[8]);
  59.  
  60.   cout << "Value at head is:   " << *L.first()  << "\n";
  61.   cout << "Value at tail is:   " << *L.last()  << "\n";
  62.   cout << "Number of links is: " << L.entries() << "\n";
  63.  
  64.   cout << "Now remove and print each link from head:\n";
  65.   while ( !L.isEmpty() )  cout << *(L.get()) << "\n";
  66.  
  67.   cout << "Remake list L, insert: 0 1 2 3 4 5\n";
  68.   L.insert(&idata[0]);
  69.   L.insert(&idata[1]);
  70.   L.insert(&idata[2]);
  71.   L.insert(&idata[3]);
  72.   L.insert(&idata[4]);
  73.   L.insert(&idata[5]);
  74.  
  75.   cout << "\nConstruct an iterator for the linked-list.\n";
  76.   GDlistIterator(int) c(L);
  77.  
  78.   // Exercise the cursor:
  79.   cout << "Cursor should point to: 5\n";
  80.   cout << " *c.key();\t" << *  c.key() << "\n";
  81.   cout << "Advance cursor 1 link.  Should point to: 0\n";
  82.   ++c;
  83.   cout << " *c.key();\t" << *c.key() << "\n";
  84.   cout << "Move cursor back 1 link.  Should point to: 5\n";
  85.   --c;
  86.   cout << " *c.key();\t" << *c.key() << "\n";
  87.   cout << "Move cursor to head of list.  Should point to: 0\n";
  88.   c.toFirst();
  89.   cout << " *c.key();\t" << *c.key() << "\n";
  90.   cout << "Delete current item (which is 0).  Should point to 1.\n";
  91.   c.remove();
  92.   cout << " *c.key();\t" << *c.key() << "\n";
  93.   cout << "Move cursor to find '4' and print it out:\n";
  94.   c.findNextReference(&idata[4]);
  95.   cout << " *c.key();\t" << *c.key() << "\n";
  96.  
  97.   // Use the operator () to move through the list:
  98.   // first, reset the cursor:
  99.   c.reset();
  100.  
  101.   cout << "Now reset the iterator and use operator() to move through the list.\n";
  102.   cout << "Should read 1 2 3 4 5\n";
  103.   const int* v;
  104.   while( (v = c()) != 0 )
  105.     cout << *v << "\n";
  106.  
  107.   return 0;
  108. }
  109.