#include "Queue.h" #include Queue& Queue::insert(const ElemType& el) { Node* pNode = new Node(el); if (isEmpty()) front = back = pNode; else { back->next = pNode; back = pNode; } return *this; } ElemType Queue::remove() { if (isEmpty()) { cerr << "Cannot remove an element from an empty queue" << endl; exit(1); } Node* pNode = front; front = front->next; ElemType retElem(pNode->elem); delete pNode; return retElem; } // Commented out to highlight the error in calling // the class with a dynamic data member via a pass // by reference parameter and using the default copy // constuctor. In production code, this mention needs // to exist. //Queue::Queue (const Queue& q) //{ //front = back = NULL; //Node* pNode = q.front; //while (pNode != NULL) //{ //insert(pNode->elem); //pNode = pNode->next; //} //} Queue::~Queue() { cout << "In ~Queue() for: " << *this << endl; while (! isEmpty() ) remove(); } Queue& Queue::operator= (const Queue& rhs) { if (this == &rhs) return *this; // deallocated the current Queue while (! isEmpty() ) remove(); front = back = NULL; Node* pNode = rhs.front; while (pNode != NULL) { insert(pNode->elem); pNode = pNode->next; } return *this; } ostream& operator<< (ostream& ostr, Queue &rhs) { bool firstLoop = true; Node* pNode = rhs.front; while (pNode != NULL) { if (firstLoop) firstLoop = false; else ostr << "->"; ostr << pNode->elem; pNode = pNode->next; } ostr << "."; return ostr; }