Move preprocessor TYPE define to header files
This commit is contained in:
parent
23eb29ea0d
commit
e4e1fd09d6
|
@ -23,15 +23,15 @@
|
||||||
template <typename T>
|
template <typename T>
|
||||||
DoubleList<T>::DoubleList(const DoubleList& rhs)
|
DoubleList<T>::DoubleList(const DoubleList& rhs)
|
||||||
{
|
{
|
||||||
Node<T> *cp = rhs.head;
|
Node *cp = rhs.head;
|
||||||
Node<T> *tempHead;
|
Node *tempHead;
|
||||||
if (cp == NULL) head = NULL;
|
if (cp == NULL) head = NULL;
|
||||||
else {
|
else {
|
||||||
head = new Node<T>(cp->data);
|
head = new Node(cp->data);
|
||||||
tempHead = head;
|
tempHead = head;
|
||||||
while (cp->next != NULL) {
|
while (cp->next != NULL) {
|
||||||
cp = cp->next;
|
cp = cp->next;
|
||||||
head->next = new Node<T>(cp->data);
|
head->next = new Node(cp->data);
|
||||||
head = head->next;
|
head = head->next;
|
||||||
}
|
}
|
||||||
head = tempHead;
|
head = tempHead;
|
||||||
|
@ -141,7 +141,7 @@ bool DoubleList<T>::replace(T val, T key)
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void DoubleList<T>::makeEmpty()
|
void DoubleList<T>::makeEmpty()
|
||||||
{
|
{
|
||||||
Node<T> *nextNode, *temp;
|
Node *nextNode, *temp;
|
||||||
|
|
||||||
if (head == NULL) return;
|
if (head == NULL) return;
|
||||||
nextNode = head->next;
|
nextNode = head->next;
|
||||||
|
@ -203,7 +203,7 @@ void DoubleList<T>::print() const
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool DoubleList<T>::find(T val) const
|
bool DoubleList<T>::find(T val) const
|
||||||
{
|
{
|
||||||
Node<T> *result = find(val, head);
|
Node *result = find(val, head);
|
||||||
if( result == NULL) {
|
if( result == NULL) {
|
||||||
std::cout << "[" << val << "] Was not found in our list\n";
|
std::cout << "[" << val << "] Was not found in our list\n";
|
||||||
return false;
|
return false;
|
||||||
|
@ -227,9 +227,9 @@ bool DoubleList<T>::find(T val) const
|
||||||
* @return false If the value could not be inserted
|
* @return false If the value could not be inserted
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool DoubleList<T>::insert(T val, Node<T> *&head)
|
bool DoubleList<T>::insert(T val, Node *&head)
|
||||||
{
|
{
|
||||||
Node<T> *newNode = new Node<T>(val);
|
Node *newNode = new Node(val);
|
||||||
// If the list is not empty, update next pointer to head node
|
// If the list is not empty, update next pointer to head node
|
||||||
if (!isEmpty()) {
|
if (!isEmpty()) {
|
||||||
newNode->next = head;
|
newNode->next = head;
|
||||||
|
@ -251,14 +251,14 @@ bool DoubleList<T>::insert(T val, Node<T> *&head)
|
||||||
* @return false If the value was not inserted
|
* @return false If the value was not inserted
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool DoubleList<T>::insert(T val, T key, Node<T> *&head)
|
bool DoubleList<T>::insert(T val, T key, Node *&head)
|
||||||
{
|
{
|
||||||
Node<T> *newNode = new Node<T>(val);
|
Node *newNode = new Node(val);
|
||||||
if (isEmpty()) return false;
|
if (isEmpty()) return false;
|
||||||
// Let insert() handle inserting at the head
|
// Let insert() handle inserting at the head
|
||||||
else if (head->data == key) return insert(val, head);
|
else if (head->data == key) return insert(val, head);
|
||||||
|
|
||||||
Node<T> *keyNode = find(key, head);
|
Node *keyNode = find(key, head);
|
||||||
// If there was no keyNode found, the key does is not in our list
|
// 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
|
// Don't insert anything, return false and let caller decide whats next
|
||||||
if (keyNode == NULL) return false;
|
if (keyNode == NULL) return false;
|
||||||
|
@ -281,7 +281,7 @@ bool DoubleList<T>::insert(T val, T key, Node<T> *&head)
|
||||||
* @return false If the value has not been removed from the list
|
* @return false If the value has not been removed from the list
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool DoubleList<T>::remove(T val, Node<T> *&head)
|
bool DoubleList<T>::remove(T val, Node *&head)
|
||||||
{
|
{
|
||||||
if (head == NULL) return false;
|
if (head == NULL) return false;
|
||||||
else if (head->data == val) {
|
else if (head->data == val) {
|
||||||
|
@ -289,9 +289,9 @@ bool DoubleList<T>::remove(T val, Node<T> *&head)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node<T> *keyNode = find(val, head);
|
Node *keyNode = find(val, head);
|
||||||
if (keyNode == NULL) return false;
|
if (keyNode == NULL) return false;
|
||||||
Node<T> *gtfo = keyNode;
|
Node *gtfo = keyNode;
|
||||||
if (keyNode->next != NULL) keyNode->next->prev = keyNode->prev;
|
if (keyNode->next != NULL) keyNode->next->prev = keyNode->prev;
|
||||||
if (keyNode->prev != NULL) keyNode->prev->next = keyNode->next;
|
if (keyNode->prev != NULL) keyNode->prev->next = keyNode->next;
|
||||||
delete gtfo;
|
delete gtfo;
|
||||||
|
@ -309,9 +309,9 @@ bool DoubleList<T>::remove(T val, Node<T> *&head)
|
||||||
* @return false If the key has not been replaced by val within the list
|
* @return false If the key has not been replaced by val within the list
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool DoubleList<T>::replace(T val, T key, Node<T> *&head)
|
bool DoubleList<T>::replace(T val, T key, Node *&head)
|
||||||
{
|
{
|
||||||
Node<T> *replacee = find(key, head);
|
Node *replacee = find(key, head);
|
||||||
if (replacee == NULL) return false;
|
if (replacee == NULL) return false;
|
||||||
replacee->data = val;
|
replacee->data = val;
|
||||||
return true;
|
return true;
|
||||||
|
@ -325,13 +325,13 @@ bool DoubleList<T>::replace(T val, T key, Node<T> *&head)
|
||||||
* @return DoubleList::Node* A pointer to the Node containing the search value
|
* @return DoubleList::Node* A pointer to the Node containing the search value
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
DoubleList<T>::Node<T>* DoubleList<T>::find(T val, Node<T> *start) const
|
typename DoubleList<T>::Node* DoubleList<T>::find(T val, Node *start) const
|
||||||
{
|
{
|
||||||
// If given a NULL list, return NULL
|
// If given a NULL list, return NULL
|
||||||
// If given a head which contains the requested value, return the foundNode
|
// If given a head which contains the requested value, return the foundNode
|
||||||
if (start == NULL || start->data == val) return start;
|
if (start == NULL || start->data == val) return start;
|
||||||
|
|
||||||
Node<T> *foundNode = start;
|
Node *foundNode = start;
|
||||||
while (foundNode->next != NULL) {
|
while (foundNode->next != NULL) {
|
||||||
foundNode = foundNode->next;
|
foundNode = foundNode->next;
|
||||||
if (foundNode->data == val) return foundNode;
|
if (foundNode->data == val) return foundNode;
|
||||||
|
@ -346,9 +346,9 @@ DoubleList<T>::Node<T>* DoubleList<T>::find(T val, Node<T> *start) const
|
||||||
* @param start The Node to begin traversing output from
|
* @param start The Node to begin traversing output from
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void DoubleList<T>::print(Node<T> *start) const
|
void DoubleList<T>::print(Node *start) const
|
||||||
{
|
{
|
||||||
Node<T> *temp = start;
|
Node *temp = start;
|
||||||
std::cout << "List Contents: ";
|
std::cout << "List Contents: ";
|
||||||
while (temp != NULL) {
|
while (temp != NULL) {
|
||||||
std::cout << temp->data << " | ";
|
std::cout << temp->data << " | ";
|
||||||
|
@ -357,6 +357,4 @@ void DoubleList<T>::print(Node<T> *start) const
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
template class DoubleList<int>;
|
template class DoubleList<TYPE>;
|
||||||
template class DoubleList<float>;
|
|
||||||
template class DoubleList<std::string>;
|
|
||||||
|
|
|
@ -13,6 +13,9 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
#define TYPE std::string
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class DoubleList {
|
class DoubleList {
|
||||||
public:
|
public:
|
||||||
|
@ -31,20 +34,19 @@ class DoubleList {
|
||||||
bool find(T val) const;
|
bool find(T val) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename TY>
|
|
||||||
struct Node {
|
struct Node {
|
||||||
TY data;
|
TYPE data;
|
||||||
Node *next, *prev;
|
Node *next, *prev;
|
||||||
Node(): data(), next(NULL), prev(NULL) {};
|
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<T> *head;
|
Node *head;
|
||||||
bool insert(T val, Node<T> *&head);
|
bool insert(T val, Node *&head);
|
||||||
bool insert(T val, T key, Node<T> *&head);
|
bool insert(T val, T key, Node *&head);
|
||||||
bool remove(T val, Node<T> *&head);
|
bool remove(T val, Node *&head);
|
||||||
bool replace(T val, T key, Node<T> *&head);
|
bool replace(T val, T key, Node *&head);
|
||||||
Node<T>* find(T val, Node<T> *start) const;
|
Node* find(T val, Node *start) const;
|
||||||
void print(Node<T> *start) const;
|
void print(Node *start) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
#include "doublelist.h"
|
#include "doublelist.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define TYPE std::string
|
|
||||||
|
|
||||||
enum OPS {
|
enum OPS {
|
||||||
EXIT, INSERT, INSERTAT, EMPTY, PEEK, PRINT, FIND, REMOVE, REPLACE
|
EXIT, INSERT, INSERTAT, EMPTY, PEEK, PRINT, FIND, REMOVE, REPLACE
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
enum OPS {
|
enum OPS {
|
||||||
|
EXIT, ENQUEUE, DEQUEUE, NEXT, PRINT, EMPTY
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
|
|
@ -23,19 +23,19 @@
|
||||||
template<typename T>
|
template<typename T>
|
||||||
QueueList<T>::QueueList(const QueueList<T>& rhs)
|
QueueList<T>::QueueList(const QueueList<T>& rhs)
|
||||||
{
|
{
|
||||||
Node<T> *cp = rhs.head;
|
Node *cp = rhs.head;
|
||||||
Node<T> *tempHead;
|
Node *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<T>(cp->data);
|
head = new Node(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<T>(cp->data);
|
head->next = new Node(cp->data);
|
||||||
head = head->next;
|
head = head->next;
|
||||||
}
|
}
|
||||||
tail = head;
|
tail = head;
|
||||||
|
@ -150,7 +150,7 @@ void QueueList<T>::print() const
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void QueueList<T>::makeEmpty()
|
void QueueList<T>::makeEmpty()
|
||||||
{
|
{
|
||||||
Node<T> *nextNode, *temp;
|
Node *nextNode, *temp;
|
||||||
|
|
||||||
if (head == NULL) std::cout << "Our queue is empty...\n";
|
if (head == NULL) std::cout << "Our queue is empty...\n";
|
||||||
else {
|
else {
|
||||||
|
@ -182,9 +182,9 @@ void QueueList<T>::makeEmpty()
|
||||||
* @return false If the value could not be inserted
|
* @return false If the value could not be inserted
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool QueueList<T>::enqueue(T val, Node<T> *&tail)
|
bool QueueList<T>::enqueue(T val, Node *&tail)
|
||||||
{
|
{
|
||||||
Node<T> *newNode = new Node<T>(val);
|
Node *newNode = new Node(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;
|
||||||
|
@ -202,10 +202,10 @@ bool QueueList<T>::enqueue(T val, Node<T> *&tail)
|
||||||
* @return T The value held at the node removed
|
* @return T The value held at the node removed
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T QueueList<T>::dequeue(Node<T> *&head)
|
T QueueList<T>::dequeue(Node *&head)
|
||||||
{
|
{
|
||||||
// We already know the queue is not empty from public dequeue()
|
// We already know the queue is not empty from public dequeue()
|
||||||
Node<T> *temp = head;
|
Node *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
|
||||||
T data = head->data;
|
T data = head->data;
|
||||||
|
|
||||||
|
@ -230,9 +230,9 @@ T QueueList<T>::dequeue(Node<T> *&head)
|
||||||
* @param start The Node to begin traversing output from
|
* @param start The Node to begin traversing output from
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void QueueList<T>::print(Node<T> *start) const
|
void QueueList<T>::print(Node *start) const
|
||||||
{
|
{
|
||||||
Node<T> *temp = start;
|
Node *temp = start;
|
||||||
std::cout << "Queue Contents: ";
|
std::cout << "Queue Contents: ";
|
||||||
while (temp != NULL) {
|
while (temp != NULL) {
|
||||||
std::cout << temp->data << " | ";
|
std::cout << temp->data << " | ";
|
||||||
|
@ -249,9 +249,9 @@ void QueueList<T>::print(Node<T> *start) const
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void QueueList<T>::makeEmpty(Node<T> *&head)
|
void QueueList<T>::makeEmpty(Node *&head)
|
||||||
{
|
{
|
||||||
Node<T> *nextNode, *temp;
|
Node *nextNode, *temp;
|
||||||
|
|
||||||
if (head == NULL) return;
|
if (head == NULL) return;
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -31,18 +31,17 @@ class QueueList {
|
||||||
void makeEmpty();
|
void makeEmpty();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<typename TY>
|
|
||||||
struct Node {
|
struct Node {
|
||||||
TY data;
|
TYPE data;
|
||||||
Node *next;
|
Node *next;
|
||||||
Node(): data(), next(NULL) {};
|
Node(): data(), next(NULL) {};
|
||||||
Node(TY val): data(val), next(NULL) {};
|
Node(TYPE val): data(val), next(NULL) {};
|
||||||
};
|
};
|
||||||
Node<T> *head, *tail;
|
Node *head, *tail;
|
||||||
bool enqueue(T val, Node<T> *&head);
|
bool enqueue(T val, Node *&head);
|
||||||
T dequeue(Node<T> *&tail);
|
T dequeue(Node *&tail);
|
||||||
void print(Node<T> *start) const;
|
void print(Node *start) const;
|
||||||
void makeEmpty(Node<T> *&head);
|
void makeEmpty(Node *&head);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Input the type we want to use within our vector here
|
// Input the type we want to use within our vector here
|
||||||
#define TYPE std::string
|
|
||||||
|
|
||||||
enum OPS {
|
enum OPS {
|
||||||
EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT
|
EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT
|
||||||
|
|
|
@ -316,6 +316,4 @@ void Vector<T>::print(T *data) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate relevant type templates for this class
|
// Instantiate relevant type templates for this class
|
||||||
template class Vector<int>;
|
template class Vector<TYPE>;
|
||||||
template class Vector<float>;
|
|
||||||
template class Vector<std::string>;
|
|
||||||
|
|
|
@ -14,6 +14,9 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
#define TYPE std::string
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Vector {
|
class Vector {
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in New Issue