Code /* stackd.h CIS 250 Dave Klick Definition and implementation of a templated stack class with dynamic memory allocation. */ #ifndef __STACKD_H_ #define __STACKD_H_ #include template class stackd { private: struct Node { T data; Node* next; }; Node* head; public: stackd(); stackd(const stackd& s); ~stackd(); bool isEmpty() const; bool isFull() const; bool push(const T data); bool pop(T& data); stackd& operator=(const stackd& s); }; template stackd::stackd() { head = NULL; } template stackd::stackd(const stackd& s) { Node* p = s.head; Node* prev = NULL; while (p != NULL) { // create new node Node* n = new Node(); if (n == NULL) return; // unrecoverable error n->data = p->data; n->next = NULL; // set up pointers if (prev == NULL) head = n; // first node in stack/list else prev->next = n; // get ready for next pass prev = n; p = p->next; } } template stackd& stackd::operator=(const stackd& s) { if (this == &s) return *this; T tmp; while (!isEmpty()) pop(tmp); Node* p = s.head; Node* prev = NULL; while (p != NULL) { // create new node Node* n = new Node(); if (n == NULL) return *this; // unrecoverable error n->data = p->data; n->next = NULL; // set up pointers if (prev == NULL) head = n; // first node in stack/list else prev->next = n; // get ready for next pass prev = n; p = p->next; } return *this; } template stackd::~stackd() { T tmp; while (!isEmpty()) pop(tmp); } template bool stackd::isEmpty() const { return head==NULL; } template bool stackd::push(const T data) { Node* n = new Node(); if (n==NULL) return false; n->data = data; n->next = head; head = n; return true; } template bool stackd::pop(T& data) { if (isEmpty()) return false; Node* tmp = head; data = head->data; head = head->next; delete tmp; return true; } #endif