Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- template <typename Type>
- class DynArrBasedStack {
- private:
- Type *mStack;
- int mSize;
- int mItemCount;
- public:
- DynArrBasedStack(int s);
- Type pop();
- void push(const Type &itemToAdd);
- bool isEmpty() { return mItemCount == 0; }
- bool isFull() { return mItemCount == mSize; }
- ~DynArrBasedStack();
- };
- template<typename Type>
- DynArrBasedStack<Type>::DynArrBasedStack(int s) {
- mSize = s;
- mItemCount = 0;
- mStack = new Type[mItemCount];
- }
- template<typename Type>
- Type DynArrBasedStack<Type>::pop() {
- if (isEmpty())
- throw("empty stack exception");
- Type temp = mStack[mItemCount - 1];
- mItemCount--;
- Type *newStack = new Type[mItemCount];
- for (int i = 0; i < mItemCount; i++)
- newStack[i] = mStack[i];
- mStack = newStack;
- return temp;
- }
- template<typename Type>
- void DynArrBasedStack<Type>::push(const Type& itemToAdd) {
- if (isFull())
- throw("full stack exception");
- mItemCount++;
- Type *newStack = new Type[mItemCount];
- for (int i = 0; i < mItemCount - 1; i++) { //assinging values from old array to new one
- newStack[i] = mStack[i];
- }
- newStack[mItemCount - 1] = itemToAdd; // adding new item
- mStack = newStack;
- }
- template<typename Type>
- DynArrBasedStack<Type>::~DynArrBasedStack() {
- delete[] mStack;
- }
Add Comment
Please, Sign In to add comment