diff --git a/data_structures/queue/CMakeLists.txt b/data_structures/queue/CMakeLists.txt index 969936730..434d3bb45 100644 --- a/data_structures/queue/CMakeLists.txt +++ b/data_structures/queue/CMakeLists.txt @@ -1,5 +1,7 @@ +add_library(libqueue OBJECT queue.h queue.cpp) add_executable( test_queue - queue.cpp test_queue.cpp ) +target_link_libraries(test_queue PUBLIC libqueue) + install(TARGETS test_queue DESTINATION "bin/data_structures") diff --git a/data_structures/queue/queue.cpp b/data_structures/queue/queue.cpp index a7055bb14..9d5f9b9df 100644 --- a/data_structures/queue/queue.cpp +++ b/data_structures/queue/queue.cpp @@ -1,8 +1,7 @@ -#include "queue.h" -#include -#include +#include "./queue.h" -using namespace std; +#include +#include /* Default constructor*/ template @@ -20,13 +19,13 @@ queue::~queue() {} template void queue::display() { node *current = queueFront; - cout << "Front --> "; + std::cout << "Front --> "; while (current != NULL) { - cout << current->data << " "; + std::cout << current->data << " "; current = current->next; } - cout << endl; - cout << "Size of queue: " << size << endl; + std::cout << std::endl; + std::cout << "Size of queue: " << size << std::endl; } /* Determine whether the queue is empty */ @@ -75,6 +74,10 @@ void queue::deQueue() { delete temp; size--; } else { - cout << "Queue is empty !" << endl; + std::cout << "Queue is empty !" << std::endl; } } + +#include +/** force instantiate to export the type class */ +template class queue; diff --git a/data_structures/queue/queue.h b/data_structures/queue/queue.h index d1305fc81..d55ee3864 100644 --- a/data_structures/queue/queue.h +++ b/data_structures/queue/queue.h @@ -1,31 +1,31 @@ /* This class specifies the basic operation on a queue as a linked list */ -#ifndef QUEUE_H -#define QUEUE_H +#ifndef DATA_STRUCTURES_QUEUE_QUEUE_H_ +#define DATA_STRUCTURES_QUEUE_QUEUE_H_ -/* Definition of the node */ +/** Definition of the node */ template struct node { Kind data; node *next; }; -/* Definition of the queue class */ +/** Definition of the queue class */ template class queue { public: - void display(); /* Show queue */ - queue(); /* Default constructor*/ - ~queue(); /* Destructor */ - bool isEmptyQueue(); /* Determine whether the queue is empty */ - void enQueue(Kind item); /* Add new item to the queue */ - Kind front(); /* Return the first element of the queue */ - void deQueue(); /* Remove the top element of the queue */ + void display(); /**< Show queue */ + queue(); /**< Default constructor*/ + ~queue(); /**< Destructor */ + bool isEmptyQueue(); /**< Determine whether the queue is empty */ + void enQueue(Kind item); /**< Add new item to the queue */ + Kind front(); /**< Return the first element of the queue */ + void deQueue(); /**< Remove the top element of the queue */ void clear(); private: - node *queueFront; /* Pointer to the front of the queue */ - node *queueRear; /* Pointer to the rear of the queue */ + node *queueFront; /**< Pointer to the front of the queue */ + node *queueRear; /**< Pointer to the rear of the queue */ int size; }; -#endif +#endif // DATA_STRUCTURES_QUEUE_QUEUE_H_ diff --git a/data_structures/queue/test_queue.cpp b/data_structures/queue/test_queue.cpp index 7f0923f78..387ccf2f7 100644 --- a/data_structures/queue/test_queue.cpp +++ b/data_structures/queue/test_queue.cpp @@ -1,41 +1,41 @@ #include #include -#include "queue.cpp" -#include "queue.h" -using namespace std; +#include "./queue.h" int main() { - queue q; - cout << "---------------------- Test construct ----------------------" - << endl; + queue q; + std::cout << "---------------------- Test construct ----------------------" + << std::endl; q.display(); - cout << "---------------------- Test isEmptyQueue ----------------------" - << endl; + std::cout + << "---------------------- Test isEmptyQueue ----------------------" + << std::endl; if (q.isEmptyQueue()) - cout << "PASS" << endl; + std::cout << "PASS" << std::endl; else - cout << "FAIL" << endl; - cout << "---------------------- Test enQueue ----------------------" - << endl; - cout << "After Hai, Jeff, Tom, Jkingston go into queue: " << endl; + std::cout << "FAIL" << std::endl; + std::cout << "---------------------- Test enQueue ----------------------" + << std::endl; + std::cout << "After Hai, Jeff, Tom, Jkingston go into queue: " << std::endl; q.enQueue("Hai"); q.enQueue("Jeff"); q.enQueue("Tom"); q.enQueue("Jkingston"); q.display(); - cout << "---------------------- Test front ----------------------" << endl; - string value = q.front(); + std::cout << "---------------------- Test front ----------------------" + << std::endl; + std::string value = q.front(); if (value == "Hai") - cout << "PASS" << endl; + std::cout << "PASS" << std::endl; else - cout << "FAIL" << endl; - cout << "---------------------- Test deQueue ----------------------" - << endl; + std::cout << "FAIL" << std::endl; + std::cout << "---------------------- Test deQueue ----------------------" + << std::endl; q.display(); q.deQueue(); q.deQueue(); - cout << "After Hai, Jeff left the queue: " << endl; + std::cout << "After Hai, Jeff left the queue: " << std::endl; q.display(); return 0; } diff --git a/data_structures/stk/main.cpp b/data_structures/stk/main.cpp index 11215ac4b..a6048c4c9 100644 --- a/data_structures/stk/main.cpp +++ b/data_structures/stk/main.cpp @@ -14,7 +14,7 @@ #include #include -#include "stack.h" +#include "./stack.h" int main(int argc, char* argv[]) { double GPA;