using namespace std;
// function template declaration must precede friend declaration in Queue
template <class T> class Queue;
template <class T>
std::ostream& operator<<(std::ostream&, const Queue<T>&);
// construct a Queue from a pair of iterators on some sequence
template <class It> Queue(It, It): head(0), tail(0)
{ copy_elems(beg, end); }
// copy control to manage pointers to QueueItems in the Queue
Queue(const Queue &Q): head(0), tail(0) {copy_elems(Q);}
Queue& operator=(const Queue&);
~Queue() { destroy(); }
// replace current Queue by contents delimited by a pair of iterators
// MS: requires member templates to be defined inside the class body
template <class Iter> void assign(Iter beg, Iter end)
{ destroy(); copy_elems(beg, end); }
// return element from head of Queue
// unchecked operation: front on an empty Queue is undefined
Type& front() { return head->item; }
const Type front() const { return head->item; }
void push(const Type &);// add element to back of Queue
void pop(); // remove element from head of Queue
bool empty() const { // true if no elements in the Queue
return head == 0;
}
//begin{basic-Queue}
// interface functions to Queue are unchanged
private:
// public members are ok: QueueItem is a private member of Queue
// only Queue and its friends may access the members of QueueItem
struct QueueItem {
// workaround: MS Compiler requires nested class members
// to be defined inside the class
QueueItem(const Type &t): item(t), next(0) { }
Type item; // value stored in this element
QueueItem *next; // pointer to next element in the Queue
};
QueueItem *head; // pointer to first element in Queue
QueueItem *tail; // pointer to last element in Queue
//end{basic-Queue}
// utility functions used by copy constructor, assignment and destructor
void destroy(); // delete all the elements
void copy_elems(const Queue&); // copy all elements from parameter
private:
// copy all elements from input range denoted by iterator pair
// MS: requires member templates to be defined inside the class body
template <class Iter> void copy_elems(Iter beg, Iter end)
{
while (beg != end) {
push(*beg);
++beg;
}
}
//begin{basic-Queue}
};
//end{basic-Queue}
//web-comment-begin
template <class Type>
void
Queue<Type>::copy_elems(const Queue &orig)
{
// copy elements from orig into this Queue
QueueItem *pt = orig.head;
while (pt) { // last element has next pointer == 0
push(pt->item); // copy the element
pt = pt->next; // get the next one from orig
}
}
template <class Type>
Queue<Type>& Queue<Type>::operator=(const Queue &rhs)
{
if (this != &rhs) {
// destroy elements on this Queue
destroy();
copy_elems(rhs); // copy from rhs into left-hand Queue
}
return *this;
}
template <class Type>
void
Queue<Type>::destroy()
{
while (!empty())
pop();
}
template <class Type>
void Queue<Type>::push(const Type &val)
{
// allocate a new QueueItem object
QueueItem *pt = new QueueItem(val);
// put item onto existing queue
if (empty())
head = tail = pt; // queue has only one element
else
{
tail->next = pt; // add new element to end of queue
tail = pt;
}
}
//begin{pop-defn}
template <class Type>
void Queue<Type>::pop()
{
// pop is unchecked: popping off an empty Queue is undefined
QueueItem* p = head; // keep pointer to head so can delete it
head = head->next; // head now points to next element
delete p; // delete old head element
}
//end{pop-defn}
template <class Type>
ostream& operator<<(ostream &os, const Queue<Type> &q)
{
os << "< ";
typename Queue<Type>::QueueItem *p;
for (p = q.head; p; p = p->next)
os << p->item << " ";
os <<">";
return os;
}
#define MAIN 1
#if 1==MAIN
Queue<int> queue1;
int main(int argc, const char *argv[])
{
for (int ix = 0; ix != 1024; ++ix)
queue1.push(ix);
for (int ix = 0; ix != 1024; ++ix) {
int i = queue1.front(); // check next item
if (i != ix)
cout << "Something's wrong! i = " << i
<< " ix = " << ix << endl;
queue1.pop(); // and remove it
}
if (!queue1.empty())
cout << "Queue is not empty but should be!" << endl;
else
cout << "OK, queue empty again" << endl;
for (int ix = 0; ix != 1024; ++ix)
queue1.push(ix);
Queue<int> queue2(queue1); // use copy constructor
for (int ix = 0; ix != 1024; ++ix) {
int i = queue2.front(); // check next item
if (i != ix)
cout << "Something's wrong! i = " << i
<< " ix = " << ix << endl;
queue2.pop(); // and remove it
}
if (!queue2.empty())
cout << "queue2 is not empty but should be!" << endl;
else
cout << "OK, queue2 empty again" << endl;
queue2 = queue1; // use asssignment operator
for (int ix = 0; ix != 1024; ++ix) {
int i = queue2.front();
if (i != ix)
cout << "Something's wrong! i = " << i
<< " ix = " << ix << endl;
queue2.pop();
}
if (!queue2.empty())
cout << "queue2 is not empty but should be!" << endl;
else
cout << "OK, queue2 empty again" << endl;
system("pause");
return 0;
}
#elif 2==MAIN
int main(int argc, const char *argv[])
{
// the class Queue<int> is instantiated
// new expression requires that Queue<int> be defined
Queue<int> *p_qi = new Queue<int>;
int ival;
for (ival = 0; ival != 10; ++ival)
// the member function push() is instantiated
p_qi->push(ival);
int err_cnt = 0;
for (ival = 0; ival != 10; ++ival) {
// the member function front is instantiated
int qval = p_qi->front();
if (ival != qval) err_cnt++;
p_qi->pop(); // instantiates pop
}
if (!err_cnt)
cout << "!! queue executed ok" << endl;
else
cout << "?? " << err_cnt << " Queue errors" << endl;
delete p_qi; // deletes elements in the queue
system("pause");
return 0;
}
int ival;
for (ival = 0; ival != 10; ++ival)
qi.push(ival); // instantiates Queue<int>::push(int)
cout << qi << endl;
int err_cnt = 0;
for (ival = 0; ival != 10; ++ival) {
int qval = qi.front(); // instantiates Queue<int>::front()
if (ival != qval) err_cnt++;
qi.pop(); // advance to next element
}