home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) IBM Corp. 1992 */
- /*--------------------------------------------------------------------------*\
- * *
- | Example program of Building Block STACK. |
- * *
- \*--------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <string.h>
- #include <istack.h>
- #include <istkseq.h>
-
- /*---------------------- Building Block specification ----------------------*\
- * *
- | A stack is specified, |
- | It is to be linked, unbouded and contains pointers to character strings. |
- * *
- \*--------------------------------------------------------------------------*/
-
-
- /*--------------------------------------------------------------------------*\
- * Test variables *
- \*--------------------------------------------------------------------------*/
-
- char *String[9] = {
- "the",
- "quick",
- "brown",
- "fox",
- "jumps",
- "over",
- "a",
- "lazy",
- "dog"
- };
-
- typedef IStack <char*> SimpleStack;
- typedef IIterator <char*> StackIterator;
- class PrintClass : public StackIterator
- {
- public:
- Boolean applyTo(char *&w)
- {
- printf("%s\n",w);
- return(True);
- }
- };
-
- /*--------------------------------------------------------------------------*\
- * Main program *
- \*--------------------------------------------------------------------------*/
- int main()
- {
- SimpleStack Stack1, Stack2;
- char *S;
- PrintClass Print;
-
- // We specify two stacks.
- // First all the strings are pushed on the first stack,
- // Next, they are popped from the first and pushed on the second,
- // Finally they are popped from the second and printed
- // This results are the strings printed in the original order.
-
- int i;
-
- printf("\n*** Example of STACK use ***\n");
-
- for (i = 0; i < 9; i ++) { // Put all strings in the stack
- Stack1.push(String[i]); // Add it as top of the stack
- }
-
- while (!Stack1.isEmpty()) {
- Stack1.pop(S); // Pop from stack 1
- Stack2.push(S); // Add it on top of stack 2
- }
- printf("Output using AllElementsDo():\n");
- Stack2.allElementsDo(Print);
-
- printf("----------------------------\n");
-
- while (!Stack2.isEmpty()) {
- Stack2.pop(S);
- printf("Popped from Stack 2: [%s]\n",S);
- }
-
- printf("\n*** End of example ***\n");
-
- return(0);
- }
-
-