#include <iostream>
using namespace std;
const int maxQsize = 100;
template <class QType> class Queue {
QType q[maxQsize];
int size;
int enqueueloc, dequeueloc;
public:
Queue(int len) {
if(len > maxQsize)
len = maxQsize;
else if(len <= 0)
len = 1;
size = len;
enqueueloc = dequeueloc = 0;
}
void enqueue(QType data) {
if(enqueueloc == size) {
cout << " -- Queue is full.\n";
return;
}
enqueueloc++;
q[enqueueloc] = data;
}
QType dequeue() {
if(dequeueloc == enqueueloc) {
cout << " -- Queue is empty.\n";
return 0;
}
dequeueloc++;
return q[dequeueloc];
}
};
int main()
{
Queue<int> intQueue1(10), intQueue2(10); // create two integer queues
intQueue1.enqueue(1);
intQueue1.enqueue(2);
intQueue1.enqueue(3);
intQueue2.enqueue(10);
intQueue2.enqueue(20);
intQueue2.enqueue(30);
cout << "Contents of integer queue intQueue1: ";
for(int i=0; i < 3; i++)
cout << intQueue1.dequeue() << " ";
cout << endl;
cout << "Contents of integer queue intQueue2: ";
for(int i=0; i < 3; i++)
cout << intQueue2.dequeue() << " ";
cout << endl;
Queue<double> doubleQueue1(10), doubleQueue(10); // create two double queues
doubleQueue1.enqueue(1.01);
doubleQueue1.enqueue(2.02);
doubleQueue1.enqueue(3.03);
doubleQueue.enqueue(10.01);
doubleQueue.enqueue(20.02);
doubleQueue.enqueue(30.03);
cout << "Contents of double queue doubleQueue1: ";
for(int i=0; i < 3; i++)
cout << doubleQueue1.dequeue() << " ";
cout << endl;
cout << "Contents of double queue doubleQueue: ";
for(int i=0; i < 3; i++)
cout << doubleQueue.dequeue() << " ";
cout << endl;
return 0;
}
Contents of integer queue intQueue1: 1 2 3
Contents of integer queue intQueue2: 10 20 30
Contents of double queue doubleQueue1: 1.01 2.02 3.03
Contents of double queue doubleQueue: 10.01 20.02 30.03
2.39.Your Queue |
| 2.39.1. | A template queue class. |
| 2.39.2. | queue class. |