home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / LETTERDQ / LETTERDQ.CPP < prev    next >
Text File  |  1995-03-15  |  3KB  |  100 lines

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