/* * File: queue-linked.cpp * Member function implementations for a template class * Queue ADT using a linked list representation * * From Kruse&Ryba: _Data Structure and Program Design in C++_ * * Modified: spc * * Class Invariant * * 1) For an empty queue, front = back = NULL * 2) For a non-empty queue, front points to the least-recently * enqueued item and back points to the most-recently enqueued item * 3) The item in the node pointed to by front will be the next to * be removed by a dequeue operation */ //#include "queue-linked.h" #include #include "queue-rizki.h" using namespace std; template Queue::Queue() { front = back = NULL; } template Queue::Queue(const Queue& copy) { Node *copy_node = copy.front; front = back = NULL; while (copy_node != NULL) { enqueue(copy_node->entry); copy_node = copy_node->next; } } template Queue::~Queue() { Queue_entry temp; // to satisfy compiler ... we never use this object while (!empty()) dequeue(temp); } template bool Queue::empty() const { return (front == NULL); } template void Queue::enqueue(const Queue_entry &item) { Node *new_back = new Node(item); if (new_back == NULL) throw string("out of memory"); if (back == NULL) front = back = new_back; else { back->next = new_back; back = new_back; } return; } template void Queue::dequeue(Queue_entry &item) { if (front == NULL) throw string("underflow"); item = front->entry; Node *old_front = front; front = old_front->next; if (front == NULL) back = NULL; delete old_front; return; } template void Queue::getFront(Queue_entry &item) { if (front == NULL) throw string("underflow"); item = front->entry; return; } template Queue& Queue::operator=(const Queue& copy) { Queue_entry temp; // to satisfy compiler ... we never use this object // clear out any current queue items while (!empty()) dequeue(temp); front = back = NULL; Node *copy_node = copy.front; while (copy_node != NULL) { enqueue(copy_node->entry); copy_node = copy_node->next; } return *this; } //#include "expqueue.h" #include using namespace std; template class Queue; template class Queue;