// theElevenQueueStory.cpp // Queue example for 810:052 Data Structures class // Code by Mark Jacobson -- Wednesday, March 26, 1997 // Reformatted by Eugene Wallingford -- Thursday, March 27, 1997 #include #include "cx9-3.cpp" // Berman's array-based queue definition #include "mystery.cpp" // for the revealMystery function int main() { // We uses an array of 4-digit numbers, initialized in a[]. int n = 15; int a[] = {1234, 4321, 2341, 8642, 1357, 9860, 5739, 8421, 3175, 2345, 3456, 4567, 8765, 1498, 2001}; Queue < int > qNums; // queue for the entire data set Queue < int > q[10]; // 10 different queues, used as waiting lines // -- which waiting line is used depends on // the value of the current **hot** digit // e.g., 3074 spends some time in // the q[3] queue, the q[0] queue, the q[7] queue, the q[4] queue. int whichQ; // whichQ indexes into q[0] ... q[9] int i; // loop counter for (i = 0; i < n; i++) qNums.enqueue(a[i]); for (int divisor = 1; divisor <= 1000; divisor *= 10) { while ( !qNums.isEmpty() ) { whichQ = ( qNums.front() / divisor ) % 10; q[whichQ].enqueue( qNums.dequeue() ); } for (i = 0; i < 10; i++) while (!q[i].isEmpty()) qNums.enqueue( q[i].dequeue() ); revealMystery(qNums, divisor, n); // print state of queue } for (i = 0; i < n; i++) cout << qNums.dequeue() << endl; }