From 23eb29ea0d4f8f9cc88b6b27c3d96666d486d444 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Thu, 30 Jul 2020 19:08:59 -0400 Subject: [PATCH] Add RD for a queuelist class using templates --- .../templates/stacklist/driver.cpp | 4 +- .../templates/stacklist/stacklist.cpp | 54 ++++++++++++------- .../templates/stacklist/stacklist.h | 22 ++++---- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/cpp/datastructs/templates/stacklist/driver.cpp b/cpp/datastructs/templates/stacklist/driver.cpp index 800d2b0..8a43314 100644 --- a/cpp/datastructs/templates/stacklist/driver.cpp +++ b/cpp/datastructs/templates/stacklist/driver.cpp @@ -19,10 +19,10 @@ int main() { std::cout << "Driver: \n"; - StackList testList; + StackList testList; bool exit = false; int choice = -1; - int val; + TYPE val; while (!exit) { diff --git a/cpp/datastructs/templates/stacklist/stacklist.cpp b/cpp/datastructs/templates/stacklist/stacklist.cpp index 87ee96a..40df1a8 100644 --- a/cpp/datastructs/templates/stacklist/stacklist.cpp +++ b/cpp/datastructs/templates/stacklist/stacklist.cpp @@ -21,7 +21,8 @@ * * @param rhs StackList object */ -StackList::StackList(const StackList& rhs) +template +StackList::StackList(const StackList& rhs) { Node *cp = rhs.head; Node *tempHead; @@ -50,7 +51,8 @@ StackList::StackList(const StackList& rhs) * @param rhs StackList object passed by value * @return StackList A deep copy of the rhs StackList object */ -StackList StackList::operator=(StackList rhs) +template +StackList StackList::operator=(StackList rhs) { if (this == &rhs) return *this; // Swap the pointers, moving the previous head data to the local variable rhs @@ -61,7 +63,8 @@ StackList StackList::operator=(StackList rhs) /** destructor * @brief Destroy the StackList::StackList object */ -StackList::~StackList() +template +StackList::~StackList() { makeEmpty(head); } @@ -76,7 +79,8 @@ StackList::~StackList() * * @param val The value to be inserted */ -bool StackList::push(int val) +template +bool StackList::push(T val) { bool inserted = push(val, head); if (inserted) @@ -88,35 +92,38 @@ bool StackList::push(int val) /** pop * @brief returns the value at the StackList::head * - * @return int The value held at the Node pointed to by StackList::head + * @return T The value held at the Node pointed to by StackList::head */ -int StackList::pop() +template +T StackList::pop() { if (!isEmpty()) std::cout << "[" << pop(head) << "] has been popped from our stack\n"; else std::cout << "Nothing to pop, our stack is empty...\n"; // If the stack has data we return it, otherwise we return the smallest possible int (error) - return (!isEmpty()) ? head->data : INT32_MIN; + return (!isEmpty()) ? head->data : T(); } /** top * @brief returns the value at the StackList::head * - * @return int The value held at the Node pointed to by StackList::head + * @return T The value held at the Node pointed to by StackList::head */ -int StackList::top() const +template +T StackList::top() const { if (!isEmpty()) std::cout << "[" << head->data << "] is at the top of our stack\n"; else std::cout << "Our stack is empty...\n"; // If the stack has data we return it, otherwise we return the smallest possible int (error) - return (!isEmpty()) ? head->data : INT32_MIN; + return (!isEmpty()) ? head->data : T(); } /** makeEmpty * @brief Empty this StackList object, deleting all associated Nodes */ -void StackList::makeEmpty() +template +void StackList::makeEmpty() { Node *nextNode, *temp; @@ -141,7 +148,8 @@ void StackList::makeEmpty() * @return true If the StackList::head is NULL * @return false If the StackList::head contains data */ -bool StackList::isEmpty() const +template +bool StackList::isEmpty() const { return head == NULL; } @@ -150,7 +158,8 @@ bool StackList::isEmpty() const * @brief Output the data held by the StackList object * Calls to the private print() */ -void StackList::print() const +template +void StackList::print() const { if(!isEmpty()) print(head); else std::cout << "Nothing to print, our stack is empty...\n"; @@ -169,7 +178,8 @@ void StackList::print() const * @return true If the value was inserted * @return false If the value could not be inserted */ -bool StackList::push(int val, Node *&head) +template +bool StackList::push(T val, Node *&head) { Node *newNode = new Node(val); // If the stack is not empty, update next pointer to head node @@ -183,13 +193,14 @@ bool StackList::push(int val, Node *&head) * @brief Private member to handle removing the head node from the stack * * @param head The head node of the stack - * @return The last known value held at the head node before removal + * @return T The last known value held at the head node before removal */ -int StackList::pop(Node *&head) +template +T StackList::pop(Node *&head) { // We already know the stack is not empty from public pop() Node *temp = head; - int data = temp->data; + T data = temp->data; head = head->next; delete temp; return data; @@ -200,7 +211,8 @@ int StackList::pop(Node *&head) * * @param start The Node to begin traversing output from */ -void StackList::print(Node *start) const +template +void StackList::print(Node *start) const { Node *temp = start; std::cout << "Stack Contents: "; @@ -216,9 +228,9 @@ void StackList::print(Node *start) const * Does not print any output. Avoids destructors printing to cout * * @param head The head of the stack to be deleted - * */ -void StackList::makeEmpty(Node *&head) +template +void StackList::makeEmpty(Node *&head) { Node *nextNode, *temp; @@ -236,3 +248,5 @@ void StackList::makeEmpty(Node *&head) } } + +template class StackList; diff --git a/cpp/datastructs/templates/stacklist/stacklist.h b/cpp/datastructs/templates/stacklist/stacklist.h index e590811..7d762c0 100644 --- a/cpp/datastructs/templates/stacklist/stacklist.h +++ b/cpp/datastructs/templates/stacklist/stacklist.h @@ -13,29 +13,33 @@ #include + +#define TYPE std::string + +template class StackList { public: StackList() : head(NULL) {}; - StackList(const StackList& rhs); - StackList operator=(StackList rhs); + StackList(const StackList& rhs); + StackList operator=(StackList rhs); ~StackList(); - bool push(int val); - int pop(); - int top() const; + bool push(T val); + T pop(); + T top() const; bool isEmpty() const; void print() const; void makeEmpty(); private: struct Node { - int data; + TYPE data; Node *next; Node(): data(), next(NULL) {}; - Node(int val): data(val), next(NULL) {}; + Node(TYPE val): data(val), next(NULL) {}; }; Node *head; - bool push(int val, Node *&head); - int pop(Node *&head); + bool push(T val, Node *&head); + T pop(Node *&head); void print(Node *start) const; void makeEmpty(Node *&head);