Advertisement
Guest User

stack2.template

a guest
Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. // FILE: stack2.template
  2. // TEMPLATE CLASS IMPLEMENTED: stack<Item> (see stack2.h for
  3. // documentation)
  4. // This file is included in the header file, and not compiled separately.
  5. // INVARIANT for the stack class:
  6. //   1. The items in the stack are stored in a linked list, with the top of the
  7. //      stack stored at the head node, down to the bottom of the stack at the
  8. //      final node.
  9. //   2. The member variable top_ptr is the head pointer of the linked list.
  10.    
  11. #include <cassert>  // Provides assert
  12. #include "node2.h"  // Node template class
  13.  
  14. namespace main_savitch_7B
  15. { template <class Item>
  16.     stack<Item>::stack(const stack<Item>& source)
  17.         // Library facilities used: node2.h
  18.     { main_savitch_6B::node<Item> *tail_ptr;
  19.         // Needed for argument of list_copy
  20.         main_savitch_6B::list_copy(source.top_ptr, top_ptr, tail_ptr);
  21.     }
  22.    
  23.     template <class Item>
  24.     void stack<Item>::push(const Item& entry)
  25.     // Library facilities used: node2.h
  26.     {
  27.         main_savitch_6B::list_head_insert(top_ptr, entry);
  28.     }
  29.  
  30.     template <class Item>
  31.     void stack<Item>::pop( )
  32.     // Library facilities used: cassert, node2.h
  33.     {  assert(!empty( ));
  34.         main_savitch_6B::list_head_remove(top_ptr);
  35.     }
  36.    
  37.     template <class Item>
  38.     void stack<Item>::operator =(const stack<Item>& source)
  39.     // Library facilities used: node2.h
  40.     {    main_savitch_6B::node<Item> *tail_ptr; // Needed for argument of list_copy
  41.         if (this == &source) // Handle self-assignment
  42.             return;
  43.         main_savitch_6B::list_clear(top_ptr);
  44.         main_savitch_6B::list_copy(source.top_ptr, top_ptr, tail_ptr);
  45.     }
  46.  
  47.     template <class Item>
  48.     Item stack<Item>::top( ) const     // Library facilities used: cassert
  49.     {  assert(!empty( ));
  50.         return top_ptr->data( );
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement