home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) IBM Corp. 1992 */
- /*--------------------------------------------------------------------------*\
- * *
- | Example program of Building Block dequeue
- * *
- \*--------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <string.h>
- #include <ideqseq.h>
- #include <ideque.h>
-
- /*---------------------- Building Block specification ----------------------*\
- * *
- | A deque is specified, |
- | It is to hold characters. |
- * *
- \*--------------------------------------------------------------------------*/
-
- typedef IDeque <char> Deque;
- typedef IIterator <char> CharIterator;
-
- class Print : public CharIterator
- {
- public:
- Boolean applyTo(char &c)
- {
- printf("Char in Deque == %c\n",c);
- return True;
- }
- };
-
- /*--------------------------------------------------------------------------*\
- * Test variables *
- \*--------------------------------------------------------------------------*/
-
- char *String = "teqikbonfxjme vralz ogdya eospu o wr cu h";
-
- /*--------------------------------------------------------------------------*\
- * Main program *
- \*--------------------------------------------------------------------------*/
- int main()
- {
- Deque D;
- char C;
- Boolean ReadFront = True;
-
- int i;
-
- // Put all characters in the deque,
- // Then read the Deque, switching from one side of
- // the Deque to the other side of the Deque.
-
-
- printf("\n*** Example of deque use ***\n");
-
- for (i = 0; String[i] != 0; i ++) { // For all characters:
- D.addAsLast(String[i]); // - put it in the deque
- printf("Add as Last [%c]\n",String[i]); // - and give feedback
- }
-
- Print Aprinter;
-
- D.allElementsDo(Aprinter);
-
- printf("Current number of elements in the deque: %lu\n",
- D.numberOfElements());
-
-
- while (!D.isEmpty()) // As long as deque not empty
- {
- if (ReadFront) // Read from front of Deque
- {
- C=D.firstElement(); // Get the character
- D.removeFirst(); // Delete it from the deque
- }
- else
- {
- D.lastElement(); // Get the character
- D.removeLast(); // Delete it from the deque
- }
- printf("%c",C);
- ReadFront = !ReadFront; // Switch to other end of Deque
- }
-
- printf("\n*** End of example ***\n");
-
- return(0);
- }
-