Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dynamic Stack Template Write your own version of a class template that will crea

ID: 3668982 • Letter: D

Question

Dynamic Stack Template

Write your own version of a class template that will create a dynamic stack of any data type. Be sure to include push, pop, and isEmpty methods.

The pop method should throw an exception if the stack is empty before attempting to remove the node from the list. In addition, include a copy constructor and an overloaded assignment operator.

Demonstrate the class with a driver program. The program should ask the user to enter a string. The program then uses the stack to display the string in reverse order.

You may not use containers from the STL. Place the class definition within it's own header file named MyStack.h.

A possible sample run:

> Hello, World! !dlroW ,olleH

Explanation / Answer

dynamicstack.h

#ifndef dynamicstack_h
#define dynamicstack_h
#include <bits/stdc++.h>
using namespace std;

template <class T>
class dynamicstack {
private:
   T *stack;
   int stackSize;
public:
   dynamicstack();
   dynamicstack(dynamicstack<T> &);
   void push(T);     // push element
T pop() throw(string); // pop element
bool isEmpty();           // check if stack is empty
int getSize();
T* getStackContainer();
};

#endif

dynamicstack.cpp (contains driver main)

#include "dynamicstack.h"
#include <bits/stdc++.h>
using namespace std;

template <class T>
dynamicstack<T>::dynamicstack() {
   stack = NULL;
   stackSize = 0;
}

template <class T>
dynamicstack<T>::dynamicstack(dynamicstack<T> &s) {
   T* stackPtr = s.getStackContainer();
   for (int i = 0; i < s.getSize(); ++i) {
       push(stackPtr[i]);
   }
}

template <class T>
void dynamicstack<T>::push(T newItem) {
   T* updatedStack = new T[stackSize + 1];
   for (int i = 0; i < stackSize; ++i) {
       updatedStack[i] = stack[i];
   }
   updatedStack[stackSize] = newItem;

   if (stack != NULL) {
       delete[] stack;
   }

   stack = updatedStack;
   stackSize += 1;
}

template <class T>
T dynamicstack<T>::pop() throw(string) {
   if (stackSize <= 0) throw *new string("Error popping empty stack.");
   T itemToReturn = stack[stackSize - 1];
   stackSize -= 1;
   return itemToReturn;
}

template <class T>
bool dynamicstack<T>::isEmpty() {
   return stackSize <= 0;
}

template <class T>
int dynamicstack<T>::getSize() {
   return stackSize;
}

template <class T>
T* dynamicstack<T>::getStackContainer() {
   return stack;
}


// driver
int main() {
   dynamicstack<string> s;

   cout << "Try popping empty stack" << endl;
   try {
       s.pop();
   } catch (string message) {
       cout << message << endl;
   }

   cout << "Input 5 string values" << endl;
   for (int i = 0; i < 5; ++i) {
       string temp;
       cin >> temp;
       s.push(temp);
   }

   cout << "Emptiness of stack: " << s.isEmpty() << endl;

   cout << "Printing stack values from top" << endl;
   while (!s.isEmpty()) {
       cout << s.pop() << endl;
   }

   cout << "Emptiness of stack: " << s.isEmpty() << endl;
   return 0;
}