Advertisement
Guest User

Untitled

a guest
Jan 20th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #ifndef STACK_H_
  2. #define STACK_H_
  3.  
  4. using namespace std;
  5.  
  6. template <class Type>
  7. class Stack {
  8.     private:
  9.         int max;
  10.         Type *items;
  11.         int top;
  12.     public:
  13.         Stack();
  14.         Stack(int x);
  15.         ~Stack();
  16.         bool push(const Type & item);
  17.         bool pop(Type & item);
  18.         bool isEmpty();
  19.         bool isFull();
  20.         int getMax();
  21.         int getTop();
  22.         void showItems();
  23. };
  24.  
  25. template <class Type>
  26. Stack<Type>::Stack() {
  27.     this->max = 5;
  28.     this->items = new Type[max];
  29.     this->top = 0;
  30. }
  31.  
  32. template <class Type>
  33. Stack<Type>::Stack(int x) {
  34.    this->max = x;
  35.    this->items = new Type[max];
  36.    this->top = 0;
  37. }
  38.  
  39. template <class Type>
  40. Stack<Type>::~Stack() {
  41.     delete [] this->items;  
  42. }
  43.  
  44. template <class Type>
  45. bool Stack<Type>::push(const Type & item) {
  46.     if (this->top < this->max) {
  47.         items[this->top] = item;
  48.         this->top++;
  49.         return true;
  50.     }
  51.     else
  52.         return false;  
  53. }
  54.  
  55. template <class Type>
  56. bool Stack<Type>::pop(Type & item) {
  57.     if (this->top > 0) {
  58.         this->top--;
  59.         item = items[this->top];
  60.         return true;
  61.     }
  62.     else
  63.         return false;    
  64. }
  65.  
  66. template <class Type>
  67. bool Stack<Type>::isEmpty() {
  68.     return (this->top == 0);    
  69. }
  70.  
  71. template <class Type>
  72. bool Stack<Type>::isFull() {
  73.     return (this->top == this->max);
  74. }
  75.  
  76. template <class Type>
  77. int Stack<Type>::getMax() {
  78.     return this->max;  
  79. }
  80.  
  81. template <class Type>
  82. int Stack<Type>::getTop() {
  83.     return this->top;  
  84. }
  85.  
  86. template <class Type>
  87. void Stack<Type>::showItems() {
  88.     if (this->top == 0) {
  89.         cout << "Stack empty!" << endl;
  90.         return;
  91.     }
  92.     cout << "What's in stack: ";
  93.     for (int i = 0; i < this->top; i++)
  94.         cout <<  this->items[i] << " ";
  95.     cout << endl;
  96. }
  97.  
  98. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement