home *** CD-ROM | disk | FTP | other *** search
- /*
- * Example 3: class GDlist; generic doubly-linked lists, using ints
- *
- * $Header: E:/vcs/toolexam/example3.cpv 1.1 01 Apr 1992 16:51:10 keffer $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-2311 FAX: (503) 757-7350
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/toolexam/example3.cpv $
- *
- * Rev 1.1 01 Apr 1992 16:51:10 keffer
- * Now includes <rw/xxx.h>
- *
- */
-
-
- // Include the header file for the class GDlist:
- #include <rw/gdlist.h>
- #include <rw/rstream.h>
-
- /* The class GDlist makes use of the macro declare defined in the header file
- * <generic.h> to implement the current C++ approximation to parameterized types.
- * The first argument is the class name (GDlist).
- * The second argument is the type of object being stored (int).
- */
-
- declare(GDlist,int)
-
- // Some data to be stored:
- int idata[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
-
- main()
- {
- // Construct a linked-list of pointers to ints with no links:
- // The form: GDlist(int) is converted via a macro to intGDlist.
- GDlist(int) L;
-
- cout << "Prepend ints: 4 3 2 1 0\n";
- L.prepend(&idata[4]);
- L.prepend(&idata[3]);
- L.prepend(&idata[2]);
- L.prepend(&idata[1]);
- L.prepend(&idata[0]);
-
- cout << "Insert (append) ints: 5 6 7 8\n";
- L.insert(&idata[5]);
- L.insert(&idata[6]);
- L.insert(&idata[7]);
- L.insert(&idata[8]);
-
- cout << "Value at head is: " << *L.first() << "\n";
- cout << "Value at tail is: " << *L.last() << "\n";
- cout << "Number of links is: " << L.entries() << "\n";
-
- cout << "Now remove and print each link from head:\n";
- while ( !L.isEmpty() ) cout << *(L.get()) << "\n";
-
- cout << "Remake list L, insert: 0 1 2 3 4 5\n";
- L.insert(&idata[0]);
- L.insert(&idata[1]);
- L.insert(&idata[2]);
- L.insert(&idata[3]);
- L.insert(&idata[4]);
- L.insert(&idata[5]);
-
- cout << "\nConstruct an iterator for the linked-list.\n";
- GDlistIterator(int) c(L);
-
- // Exercise the cursor:
- cout << "Cursor should point to: 5\n";
- cout << " *c.key();\t" << * c.key() << "\n";
- cout << "Advance cursor 1 link. Should point to: 0\n";
- ++c;
- cout << " *c.key();\t" << *c.key() << "\n";
- cout << "Move cursor back 1 link. Should point to: 5\n";
- --c;
- cout << " *c.key();\t" << *c.key() << "\n";
- cout << "Move cursor to head of list. Should point to: 0\n";
- c.toFirst();
- cout << " *c.key();\t" << *c.key() << "\n";
- cout << "Delete current item (which is 0). Should point to 1.\n";
- c.remove();
- cout << " *c.key();\t" << *c.key() << "\n";
- cout << "Move cursor to find '4' and print it out:\n";
- c.findNextReference(&idata[4]);
- cout << " *c.key();\t" << *c.key() << "\n";
-
- // Use the operator () to move through the list:
- // first, reset the cursor:
- c.reset();
-
- cout << "Now reset the iterator and use operator() to move through the list.\n";
- cout << "Should read 1 2 3 4 5\n";
- const int* v;
- while( (v = c()) != 0 )
- cout << *v << "\n";
-
- return 0;
- }
-