// FILE: queue4.template // TEMPLATE CLASS IMPLEMENTED: Queue (see queue4.h for documentation) // This file is included in the header file, and not compiled separately. // INVARIANT for the Queue class: // 1. The number of items in the queue is stored in the member variable // count. // 2. The items in the queue are stored in a linked list, with the front of // the queue stored at the head node, and the rear of the queue stored at // the final node. // 3. The member variable front_ptr is the head pointer of the linked list of // items. For a non-empty queue, the member variable rear_ptr is the // tail pointer of the linked list; for an empty list, we don't care // what's stored in rear_ptr. #include // Provides assert #include "node2.h" // Node template class namespace main_savitch_8C { template queue::queue( ) { count = 0; front_ptr = NULL; } template queue::queue(const queue& source) // Library facilities used: node2.h { count = source.count; list_copy(source.front_ptr, front_ptr, rear_ptr); } template queue::~queue( ) { list_clear(front_ptr); } template void queue::operator =(const queue& source) // Library facilities used: node2.h { if (this == &source) // Handle self-assignment return; list_clear(front_ptr); list_copy(source.front_ptr, front_ptr, rear_ptr); count = source.count; } template Item& queue::front( ) // Library facilities used: cassert { assert(!empty( )); return front_ptr->data( ); } template const Item& queue::front( ) const // Library facilities used: cassert { assert(!empty( )); return front_ptr->data( ); } template void queue::pop( ) // Library facilities used: cassert, node2.h { assert(!empty( )); list_head_remove(front_ptr); --count; } template void queue::push(const Item& entry) // Library facilities used: node2.h { if (empty( )) { // Insert first entry. list_head_insert(front_ptr, entry); rear_ptr = front_ptr; } else { // Insert an entry that is not the first. list_insert(rear_ptr, entry); rear_ptr = rear_ptr->link( ); } ++count; } }