Add RD of a queuelist class using templates

This commit is contained in:
Shaun Reed 2020-07-30 18:44:01 -04:00
parent 3729e15f1e
commit f7c22e4228
3 changed files with 64 additions and 44 deletions

View File

@ -12,17 +12,16 @@
#include <iostream> #include <iostream>
enum OPS { enum OPS {
EXIT, ENQUEUE, DEQUEUE, NEXT, PRINT, EMPTY
}; };
int main() int main()
{ {
std::cout << "Driver: \n"; std::cout << "Driver: \n";
QueueList testList; QueueList<TYPE> testList;
bool exit = false; bool exit = false;
int choice = -1; int choice = -1;
int val; TYPE val;
while (!exit) while (!exit)
{ {

View File

@ -10,7 +10,6 @@
#include "queuelist.h" #include "queuelist.h"
/****************************************************************************** /******************************************************************************
* Constructors, Destructors, Operators * Constructors, Destructors, Operators
*****************************************************************************/ *****************************************************************************/
@ -21,21 +20,22 @@
* *
* @param rhs QueueList object * @param rhs QueueList object
*/ */
QueueList::QueueList(const QueueList& rhs) template<typename T>
QueueList<T>::QueueList(const QueueList<T>& rhs)
{ {
Node *cp = rhs.head; Node<T> *cp = rhs.head;
Node *tempHead; Node<T> *tempHead;
// If we are copying from an empty queue, create a new QueueList with a NULL head // If we are copying from an empty queue, create a new QueueList with a NULL head
if (cp == NULL) head = NULL; if (cp == NULL) head = NULL;
else { else {
// If the queue has data, initialize a new queue with the head data // If the queue has data, initialize a new queue with the head data
head = new Node(cp->data); head = new Node<T>(cp->data);
// Keep a temporary head node so we can return to it later // Keep a temporary head node so we can return to it later
tempHead = head; tempHead = head;
while (cp->next != NULL) { while (cp->next != NULL) {
// Until we hit an end, create new nodes with the next node data // Until we hit an end, create new nodes with the next node data
cp = cp->next; cp = cp->next;
head->next = new Node(cp->data); head->next = new Node<T>(cp->data);
head = head->next; head = head->next;
} }
tail = head; tail = head;
@ -51,7 +51,8 @@ QueueList::QueueList(const QueueList& rhs)
* @param rhs QueueList object passed by value * @param rhs QueueList object passed by value
* @return QueueList A deep copy of the rhs QueueList object * @return QueueList A deep copy of the rhs QueueList object
*/ */
QueueList QueueList::operator=(QueueList rhs) template<typename T>
QueueList<T> QueueList<T>::operator=(QueueList<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
@ -62,7 +63,8 @@ QueueList QueueList::operator=(QueueList rhs)
/** destructor /** destructor
* @brief Destroy the QueueList::QueueList object * @brief Destroy the QueueList::QueueList object
*/ */
QueueList::~QueueList() template<typename T>
QueueList<T>::~QueueList()
{ {
makeEmpty(head); makeEmpty(head);
} }
@ -76,8 +78,11 @@ QueueList::~QueueList()
* @brief Queue a value to the tail of our linked list * @brief Queue a value to the tail of our linked list
* *
* @param val The value to be inserted into the queue * @param val The value to be inserted into the queue
* @return true If the value was inserted in the queue
* @return false If the value could not be queued
*/ */
bool QueueList::enqueue(int val) template<typename T>
bool QueueList<T>::enqueue(T val)
{ {
bool inserted = enqueue(val, tail); bool inserted = enqueue(val, tail);
if (inserted) if (inserted)
@ -89,29 +94,31 @@ bool QueueList::enqueue(int val)
/** dequeue /** dequeue
* @brief returns the value at the QueueList::head and moves head to the next in queue * @brief returns the value at the QueueList::head and moves head to the next in queue
* *
* @return int The value held at the Node pointed to by QueueList::head * @return T The value held at the Node pointed to by QueueList::head
*/ */
int QueueList::dequeue() template<typename T>
T QueueList<T>::dequeue()
{ {
if (!isEmpty()) if (!isEmpty())
std::cout << "[" << dequeue(head) << "] has been removed from our queue\n"; std::cout << "[" << dequeue(head) << "] has been removed from our queue\n";
else std::cout << "Nothing to dequeue, our queue is empty...\n"; else std::cout << "Nothing to dequeue, our queue is empty...\n";
// If the queue has data we return it, otherwise we return the smallest possible int (error) // If the queue has data we return it, otherwise we return the smallest possible int (error)
return (!isEmpty()) ? head->data : INT32_MIN; return (!isEmpty()) ? head->data : T();
} }
/** next /** next
* @brief returns the value at the QueueList::head * @brief returns the value at the QueueList::head
* *
* @return int The value held at the Node pointed to by QueueList::head * @return T The value held at the Node pointed to by QueueList::head
*/ */
int QueueList::next() const template<typename T>
T QueueList<T>::next() const
{ {
if (!isEmpty()) if (!isEmpty())
std::cout << "[" << head->data << "] is next in queue\n"; std::cout << "[" << head->data << "] is next in queue\n";
else std::cout << "Our queue is empty...\n"; else std::cout << "Our queue is empty...\n";
// If the queue has data we return it, otherwise we return the smallest possible int (error) // If the queue has data we return it, otherwise we return the smallest possible int (error)
return (!isEmpty()) ? head->data : INT32_MIN; return (!isEmpty()) ? head->data : T();
} }
/** isEmpty /** isEmpty
@ -120,7 +127,8 @@ int QueueList::next() const
* @return true If the QueueList::head is NULL * @return true If the QueueList::head is NULL
* @return false If the QueueList::head contains data * @return false If the QueueList::head contains data
*/ */
bool QueueList::isEmpty() const template<typename T>
bool QueueList<T>::isEmpty() const
{ {
return head == NULL; return head == NULL;
} }
@ -129,7 +137,8 @@ bool QueueList::isEmpty() const
* @brief Output the data held by the QueueList object * @brief Output the data held by the QueueList object
* Calls to the private print() * Calls to the private print()
*/ */
void QueueList::print() const template<typename T>
void QueueList<T>::print() const
{ {
if(!isEmpty()) print(head); if(!isEmpty()) print(head);
else std::cout << "Nothing to print, our queue is empty...\n"; else std::cout << "Nothing to print, our queue is empty...\n";
@ -138,9 +147,10 @@ void QueueList::print() const
/** makeEmpty /** makeEmpty
* @brief Empty this QueueList object, deleting all associated Nodes * @brief Empty this QueueList object, deleting all associated Nodes
*/ */
void QueueList::makeEmpty() template<typename T>
void QueueList<T>::makeEmpty()
{ {
Node *nextNode, *temp; Node<T> *nextNode, *temp;
if (head == NULL) std::cout << "Our queue is empty...\n"; if (head == NULL) std::cout << "Our queue is empty...\n";
else { else {
@ -171,9 +181,10 @@ void QueueList::makeEmpty()
* @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 QueueList::enqueue(int val, Node *&tail) template<typename T>
bool QueueList<T>::enqueue(T val, Node<T> *&tail)
{ {
Node *newNode = new Node(val); Node<T> *newNode = new Node<T>(val);
// If the queue is not empty, update next pointer to tail node // If the queue is not empty, update next pointer to tail node
if (!isEmpty()) { if (!isEmpty()) {
tail->next = newNode; tail->next = newNode;
@ -188,14 +199,15 @@ bool QueueList::enqueue(int val, Node *&tail)
* @brief Removes a node from the front of the queue * @brief Removes a node from the front of the queue
* *
* @param head The head of the queue to remove a node from * @param head The head of the queue to remove a node from
* @return int The value held at the node removed * @return T The value held at the node removed
*/ */
int QueueList::dequeue(Node *&head) template<typename T>
T QueueList<T>::dequeue(Node<T> *&head)
{ {
// We already know the queue is not empty from public dequeue() // We already know the queue is not empty from public dequeue()
Node *temp = head; Node<T> *temp = head;
// Store the data at the front of the queue before we delete the node // Store the data at the front of the queue before we delete the node
int data = head->data; T data = head->data;
// If there is only one item in the queue // If there is only one item in the queue
if (temp == tail) { if (temp == tail) {
@ -217,9 +229,10 @@ int QueueList::dequeue(Node *&head)
* *
* @param start The Node to begin traversing output from * @param start The Node to begin traversing output from
*/ */
void QueueList::print(Node *start) const template<typename T>
void QueueList<T>::print(Node<T> *start) const
{ {
Node *temp = start; Node<T> *temp = start;
std::cout << "Queue Contents: "; std::cout << "Queue Contents: ";
while (temp != NULL) { while (temp != NULL) {
std::cout << temp->data << " | "; std::cout << temp->data << " | ";
@ -235,9 +248,10 @@ void QueueList::print(Node *start) const
* @param head The head of the queue to be deleted * @param head The head of the queue to be deleted
* *
*/ */
void QueueList::makeEmpty(Node *&head) template<typename T>
void QueueList<T>::makeEmpty(Node<T> *&head)
{ {
Node *nextNode, *temp; Node<T> *nextNode, *temp;
if (head == NULL) return; if (head == NULL) return;
else { else {
@ -254,3 +268,5 @@ void QueueList::makeEmpty(Node *&head)
} }
} }
template class QueueList<TYPE>;

View File

@ -13,31 +13,36 @@
#include <iostream> #include <iostream>
#define TYPE std::string
template <typename T>
class QueueList { class QueueList {
public: public:
QueueList() : head(NULL), tail(NULL){}; QueueList() : head(NULL), tail(NULL){};
QueueList(const QueueList& rhs); QueueList(const QueueList<T>& rhs);
QueueList operator=(QueueList rhs); QueueList operator=(QueueList<T> rhs);
~QueueList(); ~QueueList();
bool enqueue(int val); bool enqueue(T val);
int dequeue(); T dequeue();
int next() const; T next() const;
bool isEmpty() const; bool isEmpty() const;
void print() const; void print() const;
void makeEmpty(); void makeEmpty();
private: private:
template<typename TY>
struct Node { struct Node {
int data; TY data;
Node *next; Node *next;
Node(): data(), next(NULL) {}; Node(): data(), next(NULL) {};
Node(int val): data(val), next(NULL) {}; Node(TY val): data(val), next(NULL) {};
}; };
Node *head, *tail; Node<T> *head, *tail;
bool enqueue(int val, Node *&head); bool enqueue(T val, Node<T> *&head);
int dequeue(Node *&tail); T dequeue(Node<T> *&tail);
void print(Node *start) const; void print(Node<T> *start) const;
void makeEmpty(Node *&head); void makeEmpty(Node<T> *&head);
}; };