home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / PUSHPOP.C < prev    next >
C/C++ Source or Header  |  1993-05-07  |  4KB  |  111 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |                                                               |
  20. | xstack.C  -  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 <istack.h>
  28.  
  29. typedef IStack <char*> SimpleStack;
  30.                       // The stack requires iteration to be const.
  31. typedef IConstantIterator <char*> StackIterator;
  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 StackIterator
  56. {
  57. public:
  58.    Boolean applyTo(char* const& w)
  59.       {
  60.       cout << w << "\n";
  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:\n";
  90.    Stack1.allElementsDo(Print);
  91.    cout << "----------------------------\n";
  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:\n";
  99.    Stack2.allElementsDo(Print);
  100.    cout << "----------------------------\n";
  101.  
  102.    while (!Stack2.isEmpty()) {
  103.       Stack2.pop(S);
  104.       cout << "Popped from Stack 2: " << S << "\n";
  105.       }
  106.  
  107.    return(0);
  108. }
  109.  
  110.  
  111.