/* * A program to illustrate the use of a generic Queue implemenation. * This is not adequate testing by any means. The point here is to * illustrate the use of templates. * * Author: Mark A. Sheldon, Tufts University, Fall 2016 */ #include #include #include #include "Queue.h" using namespace std; /* * A struct that represents dogs. * This illustrates that the queue implementation can be used * for more than just basic types like int, double, etc. */ struct Dog { string name; string owner; }; /* * You can overload the stream insertion operator to make printing * easier on your own types! * This function says that if someone wants to print a dog using an * output stream, here's what you do. */ ostream &operator<<(ostream &s, Dog d) { s << "[Dog \"" << d.name << "\" owned by \"" << d.owner << "\"]"; return s; } /* * An example of using try-catch to verify that an implementation * throws an exception when expected to. */ void test_cannot_dequeue_from_empty() { Queue intQ; int n; try { n = intQ.dequeue(); // should throw exception } catch (runtime_error &e) { cerr << "PASS: dequeue from empty queue detected" << endl; return; } cerr << "FAIL: queue does not detect dequeue from empty!" << endl; } int main() { Queue int_q; Queue dog_q; test_cannot_dequeue_from_empty(); /* Enqueue and dequeue 10 integers */ for (int i = 0; i < 10; ++i) int_q.enqueue(i); for (int i = 0; i < 10; ++i) cout << int_q.dequeue() << ' '; cout << endl; /* Now use the queue on Dog instances */ Dog dogs[] = {{"Humphrey", "Maya"}, {"Grey", "Raina"}, {"Shorty", "Mark"}}; int n_dogs = sizeof(dogs) / sizeof(Dog); for (int i = 0; i < n_dogs; ++i) dog_q.enqueue(dogs[i]); while (not dog_q.is_empty()) cout << dog_q.dequeue() << endl; return 0; }