Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Stack.h"
- template<typename Type>
- Stack<Type>::Stack(int size) {
- mSize = size;
- mItemCount = 0;
- mFront = mRear = nullptr;
- }
- template<typename Type>
- void Stack<Type>::push(const Type &itemToAdd) {
- if (isFull()) {
- throw("full stack exception");
- } else {
- Node *add = new Node;
- add->item = itemToAdd;
- add->next = nullptr;
- mItemCount++;
- if (mFront == nullptr) // if stack is empty
- mFront = add;
- else
- add->next = mRear; // this item now points into one earlier
- mRear = add; // add this item as last item
- }
- }
- template<typename Type>
- Type Stack<Type>::pop() {
- if (mFront == nullptr) {
- throw("empty stack exception");
- } else {
- Node *temp = mRear;
- Type returnType = temp->item;
- mItemCount--;
- mRear = mRear->next; // sets penultimate item to be last one
- if (mItemCount == 0)
- mFront = nullptr;
- delete temp;
- return returnType;
- }
- }
- template<typename Type>
- Stack<Type>::~Stack() {
- Node *temp;
- while (mFront != nullptr && !isEmpty()) {
- temp = mFront;
- mFront = mFront->next;
- delete temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement