// queue.cpp #include #include "queue.h" queue::queue() { head = NULL; tail = NULL; size = 0; } // end of queue constructor. queue::~queue() { while (!empty()) { remove(); } } // end of queue destructor. void queue::insert(itemtype anItem) { node* p = new node; p->item = anItem; p->next = NULL; if ( empty() ) { tail = head = p; } else if (anItem != tail->item) { tail = tail->next = p; } } // end of method insert(). boolean queue::empty() const { return (head == NULL); } // end of method empty(). itemtype queue::remove() { node* temp; itemtype itemVal; if (!empty()) { temp = head; head = head->next; itemVal = temp->item; if (head == NULL) tail = NULL; delete temp; } else { err(UNDERFLOW); } return itemVal; } // end of method remove(). void queue::see() const { node* temp; if (!empty()) { temp = head; while (temp != NULL) { if (temp->next == NULL) { cout << temp->item; } else { cout << temp->item << ","; } temp = temp->next; } } else { cout << "Queue is empty.\n"; } } // end of method see(). int queue::length() { node* temp; int total = 0; if (!empty()) { temp = head; while (temp != NULL) { temp = temp->next; total++; } } return total; } void queue::clear() { itemtype junk; while (!empty()) { junk = remove(); } } // end of method clear(). itemtype queue::front() const { // get the first item of the queue. if (empty()) { err(UNDERFLOW); } else { return head->item; } } // end of method front(). void queue::err(queueError err) const { cout << "\nError: "; switch (err) { case UNDERFLOW: cerr << "UNDERFLOW!"; break; case EMPTY: cerr <<"EMPTY!"; break; default: cerr << "UNKNOWN!"; } } // end of method err().