home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / pushpop / pushpop.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  3.7 KB  |  111 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*-------------------------------------------------------------*\
  19. |                                                               |
  20. | pushpop.CPP  -  Simple example for the use of the Stack.      |
  21. |                                                   """""       |
  22. \*-------------------------------------------------------------*/
  23.  
  24. #include <string.h>
  25. #include <iostream.h>
  26.                       // Let's use the default stack: IStack
  27. #include <istk.h>
  28.  
  29. typedef IStack <char*> SimpleStack;
  30.                       // The stack requires iteration to be const.
  31. typedef IConstantApplicator <char*> StackApplicator;
  32.  
  33.  
  34. /*-------------------------------------------------------------*\
  35. * Test variables to put into our Stack:                         *
  36. \*-------------------------------------------------------------*/
  37.  
  38. char *String[9] = {
  39.    "The",
  40.    "quick",
  41.    "brown",
  42.    "fox",
  43.    "jumps",
  44.    "over",
  45.    "a",
  46.    "lazy",
  47.    "dog."
  48. };
  49.  
  50.  
  51. /*-------------------------------------------------------------*\
  52. * A class to display the contents of our Stack:                 *
  53. \*-------------------------------------------------------------*/
  54.  
  55. class PrintClass : public StackApplicator
  56. {
  57. public:
  58.    IBoolean applyTo(char* const& w)
  59.       {
  60.       cout << w << endl;
  61.       return(True);
  62.       }
  63. };
  64.  
  65.  
  66. /*-------------------------------------------------------------*\
  67. * Main program                                                  *
  68. \*-------------------------------------------------------------*/
  69. int main()
  70. {
  71.    SimpleStack Stack1, Stack2;
  72.    char *S;
  73.    PrintClass Print;
  74.  
  75.    // We specify two stacks.
  76.    // First all the strings are pushed onto the first stack.
  77.    // Next, they are popped from the first and pushed onto
  78.    // the second.
  79.    // Finally they are popped from the second and printed.
  80.    // During this final print the strings must appear
  81.    // in their original order.
  82.  
  83.    int i;
  84.  
  85.    for (i = 0; i < 9; i ++) {
  86.       Stack1.push(String[i]);
  87.       }
  88.  
  89.    cout << "Contents of Stack1:" << endl;
  90.    Stack1.allElementsDo(Print);
  91.    cout << "----------------------------" << endl;
  92.  
  93.    while (!Stack1.isEmpty()) {
  94.       Stack1.pop(S);             // Pop from stack 1
  95.       Stack2.push(S);            // Add it on top of stack 2
  96.       }
  97.  
  98.    cout << "Contents of Stack2:" << endl;
  99.    Stack2.allElementsDo(Print);
  100.    cout << "----------------------------" << endl;
  101.  
  102.    while (!Stack2.isEmpty()) {
  103.       Stack2.pop(S);
  104.       cout << "Popped from Stack 2: " << S << endl;
  105.       }
  106.  
  107.    return(0);
  108. }
  109.  
  110.  
  111.