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

  1. /* Copyright (c) IBM Corp. 1992 */
  2. /*--------------------------------------------------------------------------*\ 
  3. *                                                                            *
  4. | Example program of Building Block dequeue 
  5. *                                                                            *
  6. \*--------------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <ideqseq.h>
  11. #include <ideque.h>
  12.  
  13. /*---------------------- Building Block specification ----------------------*\ 
  14. *                                                                            *
  15. | A deque is specified,                                                      |
  16. | It is to hold characters.                                                  |
  17. *                                                                            *
  18. \*--------------------------------------------------------------------------*/
  19.  
  20. typedef IDeque <char> Deque;
  21. typedef IIterator <char> CharIterator;
  22.  
  23. class Print : public CharIterator
  24. {
  25. public:
  26.    Boolean applyTo(char &c)
  27.       {
  28.       printf("Char in Deque == %c\n",c);
  29.       return True;
  30.       }
  31. };
  32.  
  33. /*--------------------------------------------------------------------------*\ 
  34. * Test variables                                                             *
  35. \*--------------------------------------------------------------------------*/
  36.  
  37. char *String = "teqikbonfxjme vralz ogdya  eospu o wr cu h";
  38.  
  39. /*--------------------------------------------------------------------------*\ 
  40. * Main program                                                               *
  41. \*--------------------------------------------------------------------------*/
  42. int main()
  43. {
  44.    Deque D;
  45.    char  C;
  46.    Boolean ReadFront = True;
  47.  
  48.    int i;
  49.  
  50.    // Put all characters in the deque,
  51.    // Then read the Deque, switching from one side of
  52.    // the Deque to the other side of the Deque.
  53.    
  54.  
  55.    printf("\n*** Example of deque use ***\n");
  56.  
  57.    for (i = 0; String[i] != 0; i ++) {          // For all characters:
  58.       D.addAsLast(String[i]);                   //    - put it in the deque
  59.       printf("Add as Last [%c]\n",String[i]);   //    - and give feedback
  60.       }
  61.  
  62.    Print Aprinter;
  63.  
  64.    D.allElementsDo(Aprinter);
  65.  
  66.    printf("Current number of elements in the deque: %lu\n",
  67.            D.numberOfElements());
  68.  
  69.  
  70.    while (!D.isEmpty())            // As long as deque not empty
  71.       {
  72.       if (ReadFront)             // Read from front of Deque
  73.          {
  74.          C=D.firstElement();          // Get the character         
  75.          D.removeFirst();        // Delete it from the deque
  76.          }
  77.       else
  78.          {
  79.          D.lastElement();           // Get the character         
  80.          D.removeLast();         // Delete it from the deque
  81.          }
  82.       printf("%c",C);
  83.       ReadFront = !ReadFront;    // Switch to other end of Deque
  84.       }
  85.  
  86.    printf("\n*** End of example ***\n");
  87.  
  88.    return(0);
  89. }
  90.  
  91.