Advertisement
Guest User

stack2.h (lined list version of stack class)

a guest
Oct 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. // FILE: stack2.h (part of the namespace main_savitch_7B)
  2. // TEMPLATE CLASS PROVIDED: stack<Item> (a stack of items)
  3. //   The template parameter, Item, is the data type of the items in the stack,
  4. //   also defined as stack<Item>::value_type.
  5. //   It may be any of the C++ built-in types (int, char, etc.), or a class
  6. //   with a default constructor, a copy constructor, and an assignment
  7. //   operator. The definition stack<Item>::size_type is the data type of
  8. //   any variable that keeps track of how many items are in a stack.
  9. //
  10. // CONSTRUCTOR for the stack<Item> template class:
  11. //   stack( )
  12. //     Postcondition: The stack has been initialized as an empty stack.
  13. //
  14. // MODIFICATION MEMBER FUNCTIONS for the stack<Item> class:
  15. //   void push(const Item& entry)
  16. //     Precondition: size( ) < CAPACITY.
  17. //     Postcondition: A new copy of entry has been pushed onto the stack.
  18. //
  19. //   void pop( )
  20. //     Precondition: size( ) > 0.
  21. //     Postcondition: The top item of the stack has been removed.
  22. //
  23. // CONSTANT MEMBER FUNCTIONS for the stack<Item> class:
  24. //   bool empty( ) const
  25. //     Postcondition: Return value is true if the stack is empty.
  26. //
  27. //   size_type size( ) const
  28. //     Postcondition: Return value is the total number of items in the stack.
  29. //
  30. //   Item top( ) const
  31. //     Precondition: size( ) > 0.
  32. //     Postcondition: The return value is the top item of the stack (but the
  33. //     stack is unchanged. This differs slightly from the STL stack (where
  34. //     the top function returns a reference to the item on top of the stack).
  35. //
  36. // VALUE SEMANTICS for the stack<Item> class:
  37. //   Assignments and the copy constructor may be used with stack<Item>
  38. //   objects.
  39. //
  40. // DYNAMIC MEMORY USAGE by the stack<Item> template class:
  41. //   If there is insufficient dynamic memory, then the following functions
  42. //   throw bad_alloc:
  43. //   the copy constructor, push, and the assignment operator.
  44. #ifndef MAIN_SAVITCH_STACK2_H
  45. #define MAIN_SAVITCH_STACK2_H
  46. #include <cstdlib>   // Provides NULL and size_t
  47. #include "node2.h"   // Node template class from Figure 6.4 on page 326
  48.  
  49. namespace main_savitch_7B
  50. {  template <class Item>
  51.     class stack
  52.     {
  53.     public:
  54.         typedef size_t size_type;
  55.         typedef Item value_type;
  56.        
  57.         // CONSTRUCTORS and DESTRUCTOR
  58.         stack( ) { top_ptr = NULL; }
  59.         stack(const stack& source);
  60.         ~stack( ) { main_savitch_6B::list_clear(top_ptr); }
  61.         // MODIFICATION MEMBER FUNCTIONS
  62.         void push(const Item& entry);
  63.         void pop( );
  64.         void operator =(const stack& source);
  65.         // CONSTANT MEMBER FUNCTIONS
  66.         size_type size( ) const
  67.         { return main_savitch_6B::list_length(top_ptr); }
  68.         bool empty( ) const { return (top_ptr == NULL); }
  69.         Item top( ) const;
  70.    
  71.     private:
  72.         main_savitch_6B::node<Item> *top_ptr;
  73.         // Points to top of stack
  74.     };
  75. }
  76.  
  77. #include "stack2.template" /
  78.   //Include the implementation
  79. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement