Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STACK_H_
- #define STACK_H_
- using namespace std;
- template <class Type>
- class Stack {
- private:
- int max;
- Type *items;
- int top;
- public:
- Stack();
- Stack(int x);
- ~Stack();
- bool push(const Type & item);
- bool pop(Type & item);
- bool isEmpty();
- bool isFull();
- int getMax();
- int getTop();
- void showItems();
- };
- template <class Type>
- Stack<Type>::Stack() {
- this->max = 5;
- this->items = new Type[max];
- this->top = 0;
- }
- template <class Type>
- Stack<Type>::Stack(int x) {
- this->max = x;
- this->items = new Type[max];
- this->top = 0;
- }
- template <class Type>
- Stack<Type>::~Stack() {
- delete [] this->items;
- }
- template <class Type>
- bool Stack<Type>::push(const Type & item) {
- if (this->top < this->max) {
- items[this->top] = item;
- this->top++;
- return true;
- }
- else
- return false;
- }
- template <class Type>
- bool Stack<Type>::pop(Type & item) {
- if (this->top > 0) {
- this->top--;
- item = items[this->top];
- return true;
- }
- else
- return false;
- }
- template <class Type>
- bool Stack<Type>::isEmpty() {
- return (this->top == 0);
- }
- template <class Type>
- bool Stack<Type>::isFull() {
- return (this->top == this->max);
- }
- template <class Type>
- int Stack<Type>::getMax() {
- return this->max;
- }
- template <class Type>
- int Stack<Type>::getTop() {
- return this->top;
- }
- template <class Type>
- void Stack<Type>::showItems() {
- if (this->top == 0) {
- cout << "Stack empty!" << endl;
- return;
- }
- cout << "What's in stack: ";
- for (int i = 0; i < this->top; i++)
- cout << this->items[i] << " ";
- cout << endl;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement