Add RD for a queuelist class using templates

This commit is contained in:
Shaun Reed 2020-07-30 19:08:59 -04:00
parent 4fe1fc106b
commit 23eb29ea0d
3 changed files with 49 additions and 31 deletions

View File

@ -19,10 +19,10 @@ int main()
{ {
std::cout << "Driver: \n"; std::cout << "Driver: \n";
StackList testList; StackList<TYPE> testList;
bool exit = false; bool exit = false;
int choice = -1; int choice = -1;
int val; TYPE val;
while (!exit) while (!exit)
{ {

View File

@ -21,7 +21,8 @@
* *
* @param rhs StackList object * @param rhs StackList object
*/ */
StackList::StackList(const StackList& rhs) template <typename T>
StackList<T>::StackList(const StackList<T>& rhs)
{ {
Node *cp = rhs.head; Node *cp = rhs.head;
Node *tempHead; Node *tempHead;
@ -50,7 +51,8 @@ StackList::StackList(const StackList& rhs)
* @param rhs StackList object passed by value * @param rhs StackList object passed by value
* @return StackList A deep copy of the rhs StackList object * @return StackList A deep copy of the rhs StackList object
*/ */
StackList StackList::operator=(StackList rhs) template <typename T>
StackList<T> StackList<T>::operator=(StackList<T> rhs)
{ {
if (this == &rhs) return *this; if (this == &rhs) return *this;
// Swap the pointers, moving the previous head data to the local variable rhs // Swap the pointers, moving the previous head data to the local variable rhs
@ -61,7 +63,8 @@ StackList StackList::operator=(StackList rhs)
/** destructor /** destructor
* @brief Destroy the StackList::StackList object * @brief Destroy the StackList::StackList object
*/ */
StackList::~StackList() template <typename T>
StackList<T>::~StackList()
{ {
makeEmpty(head); makeEmpty(head);
} }
@ -76,7 +79,8 @@ StackList::~StackList()
* *
* @param val The value to be inserted * @param val The value to be inserted
*/ */
bool StackList::push(int val) template <typename T>
bool StackList<T>::push(T val)
{ {
bool inserted = push(val, head); bool inserted = push(val, head);
if (inserted) if (inserted)
@ -88,35 +92,38 @@ bool StackList::push(int val)
/** pop /** pop
* @brief returns the value at the StackList::head * @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 <typename T>
T StackList<T>::pop()
{ {
if (!isEmpty()) if (!isEmpty())
std::cout << "[" << pop(head) << "] has been popped from our stack\n"; std::cout << "[" << pop(head) << "] has been popped from our stack\n";
else std::cout << "Nothing to pop, our stack is empty...\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) // 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 /** top
* @brief returns the value at the StackList::head * @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 <typename T>
T StackList<T>::top() const
{ {
if (!isEmpty()) if (!isEmpty())
std::cout << "[" << head->data << "] is at the top of our stack\n"; std::cout << "[" << head->data << "] is at the top of our stack\n";
else std::cout << "Our stack is empty...\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) // 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 /** makeEmpty
* @brief Empty this StackList object, deleting all associated Nodes * @brief Empty this StackList object, deleting all associated Nodes
*/ */
void StackList::makeEmpty() template <typename T>
void StackList<T>::makeEmpty()
{ {
Node *nextNode, *temp; Node *nextNode, *temp;
@ -141,7 +148,8 @@ void StackList::makeEmpty()
* @return true If the StackList::head is NULL * @return true If the StackList::head is NULL
* @return false If the StackList::head contains data * @return false If the StackList::head contains data
*/ */
bool StackList::isEmpty() const template <typename T>
bool StackList<T>::isEmpty() const
{ {
return head == NULL; return head == NULL;
} }
@ -150,7 +158,8 @@ bool StackList::isEmpty() const
* @brief Output the data held by the StackList object * @brief Output the data held by the StackList object
* Calls to the private print() * Calls to the private print()
*/ */
void StackList::print() const template <typename T>
void StackList<T>::print() const
{ {
if(!isEmpty()) print(head); if(!isEmpty()) print(head);
else std::cout << "Nothing to print, our stack is empty...\n"; 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 true If the value was inserted
* @return false If the value could not be inserted * @return false If the value could not be inserted
*/ */
bool StackList::push(int val, Node *&head) template <typename T>
bool StackList<T>::push(T val, Node *&head)
{ {
Node *newNode = new Node(val); Node *newNode = new Node(val);
// If the stack is not empty, update next pointer to head node // 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 * @brief Private member to handle removing the head node from the stack
* *
* @param head The head node of 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 <typename T>
T StackList<T>::pop(Node *&head)
{ {
// We already know the stack is not empty from public pop() // We already know the stack is not empty from public pop()
Node *temp = head; Node *temp = head;
int data = temp->data; T data = temp->data;
head = head->next; head = head->next;
delete temp; delete temp;
return data; return data;
@ -200,7 +211,8 @@ int StackList::pop(Node *&head)
* *
* @param start The Node to begin traversing output from * @param start The Node to begin traversing output from
*/ */
void StackList::print(Node *start) const template <typename T>
void StackList<T>::print(Node *start) const
{ {
Node *temp = start; Node *temp = start;
std::cout << "Stack Contents: "; std::cout << "Stack Contents: ";
@ -216,9 +228,9 @@ void StackList::print(Node *start) const
* Does not print any output. Avoids destructors printing to cout * Does not print any output. Avoids destructors printing to cout
* *
* @param head The head of the stack to be deleted * @param head The head of the stack to be deleted
*
*/ */
void StackList::makeEmpty(Node *&head) template <typename T>
void StackList<T>::makeEmpty(Node *&head)
{ {
Node *nextNode, *temp; Node *nextNode, *temp;
@ -236,3 +248,5 @@ void StackList::makeEmpty(Node *&head)
} }
} }
template class StackList<TYPE>;

View File

@ -13,29 +13,33 @@
#include <iostream> #include <iostream>
#define TYPE std::string
template <typename T>
class StackList { class StackList {
public: public:
StackList() : head(NULL) {}; StackList() : head(NULL) {};
StackList(const StackList& rhs); StackList(const StackList<T>& rhs);
StackList operator=(StackList rhs); StackList operator=(StackList<T> rhs);
~StackList(); ~StackList();
bool push(int val); bool push(T val);
int pop(); T pop();
int top() const; T top() const;
bool isEmpty() const; bool isEmpty() const;
void print() const; void print() const;
void makeEmpty(); void makeEmpty();
private: private:
struct Node { struct Node {
int data; TYPE data;
Node *next; Node *next;
Node(): data(), next(NULL) {}; Node(): data(), next(NULL) {};
Node(int val): data(val), next(NULL) {}; Node(TYPE val): data(val), next(NULL) {};
}; };
Node *head; Node *head;
bool push(int val, Node *&head); bool push(T val, Node *&head);
int pop(Node *&head); T pop(Node *&head);
void print(Node *start) const; void print(Node *start) const;
void makeEmpty(Node *&head); void makeEmpty(Node *&head);