Move preprocessor TYPE define to header files
This commit is contained in:
parent
23eb29ea0d
commit
e4e1fd09d6
|
@ -23,15 +23,15 @@
|
|||
template <typename T>
|
||||
DoubleList<T>::DoubleList(const DoubleList& rhs)
|
||||
{
|
||||
Node<T> *cp = rhs.head;
|
||||
Node<T> *tempHead;
|
||||
Node *cp = rhs.head;
|
||||
Node *tempHead;
|
||||
if (cp == NULL) head = NULL;
|
||||
else {
|
||||
head = new Node<T>(cp->data);
|
||||
head = new Node(cp->data);
|
||||
tempHead = head;
|
||||
while (cp->next != NULL) {
|
||||
cp = cp->next;
|
||||
head->next = new Node<T>(cp->data);
|
||||
head->next = new Node(cp->data);
|
||||
head = head->next;
|
||||
}
|
||||
head = tempHead;
|
||||
|
@ -141,7 +141,7 @@ bool DoubleList<T>::replace(T val, T key)
|
|||
template <typename T>
|
||||
void DoubleList<T>::makeEmpty()
|
||||
{
|
||||
Node<T> *nextNode, *temp;
|
||||
Node *nextNode, *temp;
|
||||
|
||||
if (head == NULL) return;
|
||||
nextNode = head->next;
|
||||
|
@ -203,7 +203,7 @@ void DoubleList<T>::print() const
|
|||
template <typename T>
|
||||
bool DoubleList<T>::find(T val) const
|
||||
{
|
||||
Node<T> *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<T>::find(T val) const
|
|||
* @return false If the value could not be inserted
|
||||
*/
|
||||
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 (!isEmpty()) {
|
||||
newNode->next = head;
|
||||
|
@ -251,14 +251,14 @@ bool DoubleList<T>::insert(T val, Node<T> *&head)
|
|||
* @return false If the value was not inserted
|
||||
*/
|
||||
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;
|
||||
// Let insert() handle inserting at the 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
|
||||
// Don't insert anything, return false and let caller decide whats next
|
||||
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
|
||||
*/
|
||||
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;
|
||||
else if (head->data == val) {
|
||||
|
@ -289,9 +289,9 @@ bool DoubleList<T>::remove(T val, Node<T> *&head)
|
|||
return true;
|
||||
}
|
||||
|
||||
Node<T> *keyNode = find(val, head);
|
||||
Node *keyNode = find(val, head);
|
||||
if (keyNode == NULL) return false;
|
||||
Node<T> *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<T>::remove(T val, Node<T> *&head)
|
|||
* @return false If the key has not been replaced by val within the list
|
||||
*/
|
||||
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;
|
||||
replacee->data = val;
|
||||
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
|
||||
*/
|
||||
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 head which contains the requested value, return the foundNode
|
||||
if (start == NULL || start->data == val) return start;
|
||||
|
||||
Node<T> *foundNode = start;
|
||||
Node *foundNode = start;
|
||||
while (foundNode->next != NULL) {
|
||||
foundNode = foundNode->next;
|
||||
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
|
||||
*/
|
||||
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: ";
|
||||
while (temp != NULL) {
|
||||
std::cout << temp->data << " | ";
|
||||
|
@ -357,6 +357,4 @@ void DoubleList<T>::print(Node<T> *start) const
|
|||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template class DoubleList<int>;
|
||||
template class DoubleList<float>;
|
||||
template class DoubleList<std::string>;
|
||||
template class DoubleList<TYPE>;
|
||||
|
|
|
@ -13,6 +13,9 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#define TYPE std::string
|
||||
|
||||
template <typename T>
|
||||
class DoubleList {
|
||||
public:
|
||||
|
@ -31,20 +34,19 @@ class DoubleList {
|
|||
bool find(T val) const;
|
||||
|
||||
private:
|
||||
template <typename TY>
|
||||
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<T> *head;
|
||||
bool insert(T val, Node<T> *&head);
|
||||
bool insert(T val, T key, Node<T> *&head);
|
||||
bool remove(T val, Node<T> *&head);
|
||||
bool replace(T val, T key, Node<T> *&head);
|
||||
Node<T>* find(T val, Node<T> *start) const;
|
||||
void print(Node<T> *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
|
||||
|
|
|
@ -11,8 +11,6 @@
|
|||
#include "doublelist.h"
|
||||
#include <iostream>
|
||||
|
||||
#define TYPE std::string
|
||||
|
||||
enum OPS {
|
||||
EXIT, INSERT, INSERTAT, EMPTY, PEEK, PRINT, FIND, REMOVE, REPLACE
|
||||
};
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include <iostream>
|
||||
|
||||
enum OPS {
|
||||
EXIT, ENQUEUE, DEQUEUE, NEXT, PRINT, EMPTY
|
||||
};
|
||||
|
||||
int main()
|
||||
|
|
|
@ -23,19 +23,19 @@
|
|||
template<typename T>
|
||||
QueueList<T>::QueueList(const QueueList<T>& rhs)
|
||||
{
|
||||
Node<T> *cp = rhs.head;
|
||||
Node<T> *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<T>(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<T>(cp->data);
|
||||
head->next = new Node(cp->data);
|
||||
head = head->next;
|
||||
}
|
||||
tail = head;
|
||||
|
@ -150,7 +150,7 @@ void QueueList<T>::print() const
|
|||
template<typename T>
|
||||
void QueueList<T>::makeEmpty()
|
||||
{
|
||||
Node<T> *nextNode, *temp;
|
||||
Node *nextNode, *temp;
|
||||
|
||||
if (head == NULL) std::cout << "Our queue is empty...\n";
|
||||
else {
|
||||
|
@ -182,9 +182,9 @@ void QueueList<T>::makeEmpty()
|
|||
* @return false If the value could not be inserted
|
||||
*/
|
||||
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 (!isEmpty()) {
|
||||
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
|
||||
*/
|
||||
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()
|
||||
Node<T> *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<T>::dequeue(Node<T> *&head)
|
|||
* @param start The Node to begin traversing output from
|
||||
*/
|
||||
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: ";
|
||||
while (temp != NULL) {
|
||||
std::cout << temp->data << " | ";
|
||||
|
@ -249,9 +249,9 @@ void QueueList<T>::print(Node<T> *start) const
|
|||
*
|
||||
*/
|
||||
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;
|
||||
else {
|
||||
|
|
|
@ -31,18 +31,17 @@ class QueueList {
|
|||
void makeEmpty();
|
||||
|
||||
private:
|
||||
template<typename TY>
|
||||
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<T> *head, *tail;
|
||||
bool enqueue(T val, Node<T> *&head);
|
||||
T dequeue(Node<T> *&tail);
|
||||
void print(Node<T> *start) const;
|
||||
void makeEmpty(Node<T> *&head);
|
||||
Node *head, *tail;
|
||||
bool enqueue(T val, Node *&head);
|
||||
T dequeue(Node *&tail);
|
||||
void print(Node *start) const;
|
||||
void makeEmpty(Node *&head);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
#include <iostream>
|
||||
|
||||
// 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
|
||||
|
|
|
@ -316,6 +316,4 @@ void Vector<T>::print(T *data) const
|
|||
}
|
||||
|
||||
// Instantiate relevant type templates for this class
|
||||
template class Vector<int>;
|
||||
template class Vector<float>;
|
||||
template class Vector<std::string>;
|
||||
template class Vector<TYPE>;
|
||||
|
|
|
@ -14,6 +14,9 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
|
||||
#define TYPE std::string
|
||||
|
||||
template <typename T>
|
||||
class Vector {
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue