diff --git a/cpp/datastructs/templates/doublelist/doublelist.cpp b/cpp/datastructs/templates/doublelist/doublelist.cpp index 693d79d..86a9918 100644 --- a/cpp/datastructs/templates/doublelist/doublelist.cpp +++ b/cpp/datastructs/templates/doublelist/doublelist.cpp @@ -23,15 +23,15 @@ template DoubleList::DoubleList(const DoubleList& rhs) { - Node *cp = rhs.head; - Node *tempHead; + Node *cp = rhs.head; + Node *tempHead; if (cp == NULL) head = NULL; else { - head = new Node(cp->data); + head = new Node(cp->data); tempHead = head; while (cp->next != NULL) { cp = cp->next; - head->next = new Node(cp->data); + head->next = new Node(cp->data); head = head->next; } head = tempHead; @@ -141,7 +141,7 @@ bool DoubleList::replace(T val, T key) template void DoubleList::makeEmpty() { - Node *nextNode, *temp; + Node *nextNode, *temp; if (head == NULL) return; nextNode = head->next; @@ -203,7 +203,7 @@ void DoubleList::print() const template bool DoubleList::find(T val) const { - Node *result = find(val, head); + Node *result = find(val, head); if( result == NULL) { std::cout << "[" << val << "] Was not found in our list\n"; return false; @@ -227,9 +227,9 @@ bool DoubleList::find(T val) const * @return false If the value could not be inserted */ template -bool DoubleList::insert(T val, Node *&head) +bool DoubleList::insert(T val, Node *&head) { - Node *newNode = new Node(val); + Node *newNode = new Node(val); // If the list is not empty, update next pointer to head node if (!isEmpty()) { newNode->next = head; @@ -251,14 +251,14 @@ bool DoubleList::insert(T val, Node *&head) * @return false If the value was not inserted */ template -bool DoubleList::insert(T val, T key, Node *&head) +bool DoubleList::insert(T val, T key, Node *&head) { - Node *newNode = new Node(val); + Node *newNode = new Node(val); if (isEmpty()) return false; // Let insert() handle inserting at the head else if (head->data == key) return insert(val, head); - Node *keyNode = find(key, head); + Node *keyNode = find(key, head); // If there was no keyNode found, the key does is not in our list // Don't insert anything, return false and let caller decide whats next if (keyNode == NULL) return false; @@ -281,7 +281,7 @@ bool DoubleList::insert(T val, T key, Node *&head) * @return false If the value has not been removed from the list */ template -bool DoubleList::remove(T val, Node *&head) +bool DoubleList::remove(T val, Node *&head) { if (head == NULL) return false; else if (head->data == val) { @@ -289,9 +289,9 @@ bool DoubleList::remove(T val, Node *&head) return true; } - Node *keyNode = find(val, head); + Node *keyNode = find(val, head); if (keyNode == NULL) return false; - Node *gtfo = keyNode; + Node *gtfo = keyNode; if (keyNode->next != NULL) keyNode->next->prev = keyNode->prev; if (keyNode->prev != NULL) keyNode->prev->next = keyNode->next; delete gtfo; @@ -309,9 +309,9 @@ bool DoubleList::remove(T val, Node *&head) * @return false If the key has not been replaced by val within the list */ template -bool DoubleList::replace(T val, T key, Node *&head) +bool DoubleList::replace(T val, T key, Node *&head) { - Node *replacee = find(key, head); + Node *replacee = find(key, head); if (replacee == NULL) return false; replacee->data = val; return true; @@ -325,13 +325,13 @@ bool DoubleList::replace(T val, T key, Node *&head) * @return DoubleList::Node* A pointer to the Node containing the search value */ template -DoubleList::Node* DoubleList::find(T val, Node *start) const +typename DoubleList::Node* DoubleList::find(T val, Node *start) const { // If given a NULL list, return NULL // If given a head which contains the requested value, return the foundNode if (start == NULL || start->data == val) return start; - Node *foundNode = start; + Node *foundNode = start; while (foundNode->next != NULL) { foundNode = foundNode->next; if (foundNode->data == val) return foundNode; @@ -346,9 +346,9 @@ DoubleList::Node* DoubleList::find(T val, Node *start) const * @param start The Node to begin traversing output from */ template -void DoubleList::print(Node *start) const +void DoubleList::print(Node *start) const { - Node *temp = start; + Node *temp = start; std::cout << "List Contents: "; while (temp != NULL) { std::cout << temp->data << " | "; @@ -357,6 +357,4 @@ void DoubleList::print(Node *start) const std::cout << std::endl; } -template class DoubleList; -template class DoubleList; -template class DoubleList; +template class DoubleList; diff --git a/cpp/datastructs/templates/doublelist/doublelist.h b/cpp/datastructs/templates/doublelist/doublelist.h index 394f63c..360c0ea 100644 --- a/cpp/datastructs/templates/doublelist/doublelist.h +++ b/cpp/datastructs/templates/doublelist/doublelist.h @@ -13,6 +13,9 @@ #include + +#define TYPE std::string + template class DoubleList { public: @@ -31,20 +34,19 @@ class DoubleList { bool find(T val) const; private: - template struct Node { - TY data; + TYPE data; Node *next, *prev; Node(): data(), next(NULL), prev(NULL) {}; - Node(T val): data(val), next(NULL), prev(NULL) {}; + Node(TYPE val): data(val), next(NULL), prev(NULL) {}; }; - Node *head; - bool insert(T val, Node *&head); - bool insert(T val, T key, Node *&head); - bool remove(T val, Node *&head); - bool replace(T val, T key, Node *&head); - Node* find(T val, Node *start) const; - void print(Node *start) const; + Node *head; + bool insert(T val, Node *&head); + bool insert(T val, T key, Node *&head); + bool remove(T val, Node *&head); + bool replace(T val, T key, Node *&head); + Node* find(T val, Node *start) const; + void print(Node *start) const; }; #endif diff --git a/cpp/datastructs/templates/doublelist/driver.cpp b/cpp/datastructs/templates/doublelist/driver.cpp index b03e026..1d53db8 100644 --- a/cpp/datastructs/templates/doublelist/driver.cpp +++ b/cpp/datastructs/templates/doublelist/driver.cpp @@ -11,8 +11,6 @@ #include "doublelist.h" #include -#define TYPE std::string - enum OPS { EXIT, INSERT, INSERTAT, EMPTY, PEEK, PRINT, FIND, REMOVE, REPLACE }; diff --git a/cpp/datastructs/templates/queuelist/driver.cpp b/cpp/datastructs/templates/queuelist/driver.cpp index b142d8b..ebc6f95 100644 --- a/cpp/datastructs/templates/queuelist/driver.cpp +++ b/cpp/datastructs/templates/queuelist/driver.cpp @@ -12,6 +12,7 @@ #include enum OPS { + EXIT, ENQUEUE, DEQUEUE, NEXT, PRINT, EMPTY }; int main() diff --git a/cpp/datastructs/templates/queuelist/queuelist.cpp b/cpp/datastructs/templates/queuelist/queuelist.cpp index 386b372..bf0f679 100644 --- a/cpp/datastructs/templates/queuelist/queuelist.cpp +++ b/cpp/datastructs/templates/queuelist/queuelist.cpp @@ -23,19 +23,19 @@ template QueueList::QueueList(const QueueList& rhs) { - Node *cp = rhs.head; - Node *tempHead; + Node *cp = rhs.head; + Node *tempHead; // If we are copying from an empty queue, create a new QueueList with a NULL head if (cp == NULL) head = NULL; else { // If the queue has data, initialize a new queue with the head data - head = new Node(cp->data); + head = new Node(cp->data); // Keep a temporary head node so we can return to it later tempHead = head; while (cp->next != NULL) { // Until we hit an end, create new nodes with the next node data cp = cp->next; - head->next = new Node(cp->data); + head->next = new Node(cp->data); head = head->next; } tail = head; @@ -150,7 +150,7 @@ void QueueList::print() const template void QueueList::makeEmpty() { - Node *nextNode, *temp; + Node *nextNode, *temp; if (head == NULL) std::cout << "Our queue is empty...\n"; else { @@ -182,9 +182,9 @@ void QueueList::makeEmpty() * @return false If the value could not be inserted */ template -bool QueueList::enqueue(T val, Node *&tail) +bool QueueList::enqueue(T val, Node *&tail) { - Node *newNode = new Node(val); + Node *newNode = new Node(val); // If the queue is not empty, update next pointer to tail node if (!isEmpty()) { tail->next = newNode; @@ -202,10 +202,10 @@ bool QueueList::enqueue(T val, Node *&tail) * @return T The value held at the node removed */ template -T QueueList::dequeue(Node *&head) +T QueueList::dequeue(Node *&head) { // We already know the queue is not empty from public dequeue() - Node *temp = head; + Node *temp = head; // Store the data at the front of the queue before we delete the node T data = head->data; @@ -230,9 +230,9 @@ T QueueList::dequeue(Node *&head) * @param start The Node to begin traversing output from */ template -void QueueList::print(Node *start) const +void QueueList::print(Node *start) const { - Node *temp = start; + Node *temp = start; std::cout << "Queue Contents: "; while (temp != NULL) { std::cout << temp->data << " | "; @@ -249,9 +249,9 @@ void QueueList::print(Node *start) const * */ template -void QueueList::makeEmpty(Node *&head) +void QueueList::makeEmpty(Node *&head) { - Node *nextNode, *temp; + Node *nextNode, *temp; if (head == NULL) return; else { diff --git a/cpp/datastructs/templates/queuelist/queuelist.h b/cpp/datastructs/templates/queuelist/queuelist.h index f4fa393..dfc3742 100644 --- a/cpp/datastructs/templates/queuelist/queuelist.h +++ b/cpp/datastructs/templates/queuelist/queuelist.h @@ -31,18 +31,17 @@ class QueueList { void makeEmpty(); private: - template struct Node { - TY data; + TYPE data; Node *next; Node(): data(), next(NULL) {}; - Node(TY val): data(val), next(NULL) {}; + Node(TYPE val): data(val), next(NULL) {}; }; - Node *head, *tail; - bool enqueue(T val, Node *&head); - T dequeue(Node *&tail); - void print(Node *start) const; - void makeEmpty(Node *&head); + Node *head, *tail; + bool enqueue(T val, Node *&head); + T dequeue(Node *&tail); + void print(Node *start) const; + void makeEmpty(Node *&head); }; diff --git a/cpp/datastructs/templates/vector/driver.cpp b/cpp/datastructs/templates/vector/driver.cpp index 66dbc69..0a46385 100644 --- a/cpp/datastructs/templates/vector/driver.cpp +++ b/cpp/datastructs/templates/vector/driver.cpp @@ -12,7 +12,6 @@ #include // Input the type we want to use within our vector here -#define TYPE std::string enum OPS { EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT diff --git a/cpp/datastructs/templates/vector/vector.cpp b/cpp/datastructs/templates/vector/vector.cpp index 4024506..a329bb1 100644 --- a/cpp/datastructs/templates/vector/vector.cpp +++ b/cpp/datastructs/templates/vector/vector.cpp @@ -316,6 +316,4 @@ void Vector::print(T *data) const } // Instantiate relevant type templates for this class -template class Vector; -template class Vector; -template class Vector; +template class Vector; diff --git a/cpp/datastructs/templates/vector/vector.h b/cpp/datastructs/templates/vector/vector.h index 9380b1c..834e2ca 100644 --- a/cpp/datastructs/templates/vector/vector.h +++ b/cpp/datastructs/templates/vector/vector.h @@ -14,6 +14,9 @@ #include #include + +#define TYPE std::string + template class Vector { public: