// FILE: stack4.template // TEMPLATE CLASS IMPLEMENTED: stack (see stack4.h for documentation) // This file is included in the header file, and not compiled separately. // INVARIANT for the stack class: // 1. The items in the stack are stored in a linked list, with the top of the // stack stored at the head node, down to the bottom of the stack at the // final node. // 2. The member variable top_ptr is the head pointer of the linked list. #include // Provides assert #include "node2.h" // Node template class namespace main_savitch_7B { template stack::stack(const stack& source) // Library facilities used: node2.h { main_savitch_6B::node *tail_ptr; // Needed for argument of list_copy list_copy(source.top_ptr, top_ptr, tail_ptr); } template void stack::push(const Item& entry) // Library facilities used: node2.h { list_head_insert(top_ptr, entry); } template void stack::pop( ) // Library facilities used: cassert, node2.h { assert(!empty( )); list_head_remove(top_ptr); } template void stack::operator =(const stack& source) // Library facilities used: node2.h { main_savitch_6B::node *tail_ptr; // Needed for argument of list_copy if (this == &source) // Handle self-assignment return; list_clear(top_ptr); list_copy(source.top_ptr, top_ptr, tail_ptr); } template Item& stack::top( ) // Library facilities used: cassert { assert(!empty( )); return top_ptr->data( ); } template const Item& stack::top( ) const // Library facilities used: cassert { assert(!empty( )); return top_ptr->data( ); } }