home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / bbxxsamk / xstack.c__ / XSTACK.C
Encoding:
C/C++ Source or Header  |  1992-10-26  |  2.9 KB  |  92 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2. /*--------------------------------------------------------------------------*\ 
  3. *                                                                            *
  4. | Example program of Building Block STACK.                                  |
  5. *                                                                            *
  6. \*--------------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <istack.h>
  11. #include <istkseq.h>
  12.  
  13. /*---------------------- Building Block specification ----------------------*\ 
  14. *                                                                            *
  15. | A stack is specified,                                                      |
  16. | It is to be linked, unbouded and contains pointers to character strings.   |
  17. *                                                                            *
  18. \*--------------------------------------------------------------------------*/
  19.  
  20.  
  21. /*--------------------------------------------------------------------------*\ 
  22. * Test variables                                                             *
  23. \*--------------------------------------------------------------------------*/
  24.  
  25. char *String[9] = {
  26.    "the",
  27.    "quick",
  28.    "brown",
  29.    "fox",
  30.    "jumps",
  31.    "over",
  32.    "a",
  33.    "lazy",
  34.    "dog"
  35. };
  36.  
  37. typedef IStack <char*> SimpleStack; 
  38. typedef IIterator <char*> StackIterator;
  39. class PrintClass : public StackIterator
  40. {
  41. public:
  42.    Boolean applyTo(char *&w)
  43.       {
  44.       printf("%s\n",w);
  45.       return(True);
  46.       }
  47. };
  48.  
  49. /*--------------------------------------------------------------------------*\ 
  50. * Main program                                                               *
  51. \*--------------------------------------------------------------------------*/
  52. int main()
  53. {
  54.    SimpleStack Stack1, Stack2;
  55.    char *S;
  56.    PrintClass Print;
  57.  
  58.    // We specify two stacks.
  59.    // First all the strings are pushed on the first stack,
  60.    // Next, they are popped from the first and pushed on the second,
  61.    // Finally they are popped from the second and printed
  62.    // This results are the strings printed in the original order.
  63.  
  64.    int i;
  65.  
  66.    printf("\n*** Example of STACK use ***\n");
  67.  
  68.    for (i = 0; i < 9; i ++) {                // Put all strings in the stack
  69.       Stack1.push(String[i]);                // Add it as top of the stack
  70.       }
  71.  
  72.    while (!Stack1.isEmpty()) {
  73.       Stack1.pop(S);                         // Pop from stack 1
  74.       Stack2.push(S);                        // Add it on top of stack 2
  75.       }
  76.    printf("Output using AllElementsDo():\n");
  77.    Stack2.allElementsDo(Print);
  78.  
  79.    printf("----------------------------\n");
  80.  
  81.    while (!Stack2.isEmpty()) {
  82.       Stack2.pop(S);
  83.       printf("Popped from Stack 2: [%s]\n",S);
  84.       }
  85.                
  86.    printf("\n*** End of example ***\n");
  87.  
  88.    return(0);
  89. }
  90.  
  91.  
  92.