home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / letterdq / letterdq.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  3.6 KB  |  108 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*-------------------------------------------------------------*\
  19. |                                                               |
  20. | letterdq.CPP  -  Letter Double Ended Queue                    |
  21. |                  This is an example of using a Deque.         |
  22. |                                                """""          |
  23. \*-------------------------------------------------------------*/
  24.  
  25. #include <iostream.h>
  26.  
  27. #include <idqu.h>
  28.                      // Let's use the default deque
  29. typedef IDeque <char> Deque;
  30.                      // The deque requires iteration to be const
  31. typedef IConstantApplicator <char> CharApplicator;
  32.  
  33. class Print : public CharApplicator
  34. {
  35. public:
  36.    IBoolean applyTo(char const&c)
  37.       {
  38.       cout << c;
  39.       return True;
  40.       }
  41. };
  42.  
  43. /*-------------------------------------------------------------*\
  44. | Test variables                                                |
  45. \*-------------------------------------------------------------*/
  46.  
  47. char *String = "Teqikbonfxjmsoe  aydg.o zlarv pu o wr cu h";
  48.  
  49.  
  50. /*-------------------------------------------------------------*\
  51. | Main program                                                  |
  52. \*-------------------------------------------------------------*/
  53. int main()
  54. {
  55.    Deque D;
  56.    char  C;
  57.    IBoolean ReadFront = True;
  58.  
  59.    int i;
  60.  
  61.    // Put all characters in the deque.
  62.    // Then read it, changing the end to read from
  63.    // with every character read.
  64.  
  65.    cout << endl
  66.         << "Adding characters to the back end of the deque:" << endl;
  67.  
  68.    for (i = 0; String[i] != 0; i ++) {
  69.       D.addAsLast(String[i]);
  70.       cout << String[i];
  71.       }
  72.  
  73.    cout << endl << endl
  74.         << "Current number of elements in the deque: "
  75.         <<  D.numberOfElements() << endl;
  76.  
  77.    cout << endl
  78.         << "Contents of the deque:" << endl;
  79.    Print Aprinter;
  80.    D.allElementsDo(Aprinter);
  81.  
  82.    cout << endl << endl
  83.         << "Reading from the deque one element from front, one "
  84.         << "from back, and so on:" << endl;
  85.  
  86.    while (!D.isEmpty())
  87.       {
  88.       if (ReadFront)                  // Read from front of Deque
  89.          {
  90.          C = D.firstElement();        // Get the character
  91.          D.removeFirst();             // Delete it from the Deque
  92.          }
  93.       else
  94.          {
  95.          C = D.lastElement();
  96.          D.removeLast();
  97.          }
  98.       cout << C;
  99.  
  100.       ReadFront = !ReadFront;     // Switch to other end of Deque
  101.       }
  102.  
  103.    cout << endl;
  104.  
  105.    return(0);
  106. }
  107.  
  108.