home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / PUSHPOP / PUSHPOP.CPP < prev    next >
Text File  |  1995-03-15  |  3KB  |  102 lines

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