home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / LETTERDQ.C < prev    next >
C/C++ Source or Header  |  1993-05-07  |  4KB  |  103 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. | letterdq.C  -  Letter Double Ended Queue                      |
  21. |                This is an example of using a Deque.           |
  22. |                                              """""            |
  23. \*-------------------------------------------------------------*/
  24.  
  25. #include <iostream.h>
  26.  
  27. #include <ideqseq.h>
  28. #include <ideque.h>
  29.                      // Let's use the default deque
  30. typedef IDeque <char> Deque;
  31.                      // The deque requires iteration to be const
  32. typedef IConstantIterator <char> CharIterator;
  33.  
  34. class Print : public CharIterator
  35. {
  36. public:
  37.    Boolean applyTo(char const&c)
  38.       {
  39.       cout << c;
  40.       return True;
  41.       }
  42. };
  43.  
  44. /*-------------------------------------------------------------*\
  45. | Test variables                                                |
  46. \*-------------------------------------------------------------*/
  47.  
  48. char *String = "Teqikbonfxjme vralz o.gdya  eospu o wr cu h";
  49.  
  50.  
  51. /*-------------------------------------------------------------*\
  52. | Main program                                                  |
  53. \*-------------------------------------------------------------*/
  54. int main()
  55. {
  56.    Deque D;
  57.    char  C;
  58.    Boolean ReadFront = True;
  59.  
  60.    int i;
  61.  
  62.    // Put all characters in the deque.
  63.    // Then read it, changing the end to read from
  64.    // with every character read.
  65.  
  66.    cout << "\nAdding characters to the back end of the deque:\n";
  67.  
  68.    for (i = 0; String[i] != 0; i ++) {
  69.       D.addAsLast(String[i]);
  70.       cout << String[i];
  71.       }
  72.  
  73.    cout << "\n\nCurrent number of elements in the deque: "
  74.         <<  D.numberOfElements() << "\n";
  75.  
  76.    cout << "\nContents of the deque:\n";
  77.    Print Aprinter;
  78.    D.allElementsDo(Aprinter);
  79.  
  80.    cout << "\n\nReading from the deque one element from front, one "
  81.            "from back, and so on:\n";
  82.  
  83.    while (!D.isEmpty())
  84.       {
  85.       if (ReadFront)                  // Read from front of Deque
  86.          {
  87.          C = D.firstElement();        // Get the character
  88.          D.removeFirst();             // Delete it from the Deque
  89.          }
  90.       else
  91.          {
  92.          C = D.lastElement();
  93.          D.removeLast();
  94.          }
  95.       cout << C;
  96.  
  97.       ReadFront = !ReadFront;     // Switch to other end of Deque
  98.       }
  99.  
  100.    return(0);
  101. }
  102.  
  103.