stacktp.h 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // stacktp.h -- a stack template
  2. #ifndef STACKTP_H_
  3. #define STACKTP_H_
  4. template <class Type>
  5. class Stack
  6. {
  7. private:
  8. enum {MAX = 10}; // constant specific to class
  9. Type items[MAX]; // holds stack items
  10. int top; // index for top stack item
  11. public:
  12. Stack();
  13. bool isempty();
  14. bool isfull();
  15. bool push(const Type & item); // add item to stack
  16. bool pop(Type & item); // pop top into item
  17. };
  18. template <class Type>
  19. Stack<Type>::Stack()
  20. {
  21. top = 0;
  22. }
  23. template <class Type>
  24. bool Stack<Type>::isempty()
  25. {
  26. return top == 0;
  27. }
  28. template <class Type>
  29. bool Stack<Type>::isfull()
  30. {
  31. return top == MAX;
  32. }
  33. template <class Type>
  34. bool Stack<Type>::push(const Type & item)
  35. {
  36. if (top < MAX)
  37. {
  38. items[top++] = item;
  39. return true;
  40. }
  41. else
  42. return false;
  43. }
  44. template <class Type>
  45. bool Stack<Type>::pop(Type & item)
  46. {
  47. if (top > 0)
  48. {
  49. item = items[--top];
  50. return true;
  51. }
  52. else
  53. return false;
  54. }
  55. #endif