diff --git a/data_structures/CMakeLists.txt b/data_structures/CMakeLists.txt index fab3c5642..6c0555148 100644 --- a/data_structures/CMakeLists.txt +++ b/data_structures/CMakeLists.txt @@ -18,5 +18,3 @@ foreach( testsourcefile ${APP_SOURCES} ) endforeach( testsourcefile ${APP_SOURCES} ) add_subdirectory(cll) -add_subdirectory(queue) -add_subdirectory(stk) diff --git a/data_structures/queue.h b/data_structures/queue.h new file mode 100644 index 000000000..ed97a1e8d --- /dev/null +++ b/data_structures/queue.h @@ -0,0 +1,88 @@ +/* This class specifies the basic operation on a queue as a linked list */ +#ifndef DATA_STRUCTURES_QUEUE_QUEUE_H_ +#define DATA_STRUCTURES_QUEUE_QUEUE_H_ + +#include +#include + +/** Definition of the node */ +template +struct node { + Kind data; + node *next; +}; + +/** Definition of the queue class */ +template +class queue { + public: + /** Show queue */ + void display() { + node *current = queueFront; + std::cout << "Front --> "; + while (current != NULL) { + std::cout << current->data << " "; + current = current->next; + } + std::cout << std::endl; + std::cout << "Size of queue: " << size << std::endl; + }; + + /** Default constructor*/ + queue() { + queueFront = NULL; + queueRear = NULL; + size = 0; + }; + + /** Destructor */ + ~queue(){}; + + /** Determine whether the queue is empty */ + bool isEmptyQueue() { return (queueFront == NULL); }; + + /** Add new item to the queue */ + void enQueue(Kind item) { + node *newNode; + newNode = new node; + newNode->data = item; + newNode->next = NULL; + if (queueFront == NULL) { + queueFront = newNode; + queueRear = newNode; + } else { + queueRear->next = newNode; + queueRear = queueRear->next; + } + size++; + }; + + /** Return the first element of the queue */ + Kind front() { + assert(queueFront != NULL); + return queueFront->data; + }; + + /** Remove the top element of the queue */ + void deQueue() { + node *temp; + if (!isEmptyQueue()) { + temp = queueFront; + queueFront = queueFront->next; + delete temp; + size--; + } else { + std::cout << "Queue is empty !" << std::endl; + } + }; + + /** Clear queue */ + void clear() { queueFront = NULL; }; + + private: + node *queueFront; /**< Pointer to the front of the queue */ + node *queueRear; /**< Pointer to the rear of the queue */ + int size; +}; + +#endif // DATA_STRUCTURES_QUEUE_QUEUE_H_ diff --git a/data_structures/queue/CMakeLists.txt b/data_structures/queue/CMakeLists.txt deleted file mode 100644 index 434d3bb45..000000000 --- a/data_structures/queue/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -add_library(libqueue OBJECT queue.h queue.cpp) -add_executable( test_queue - 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 deleted file mode 100644 index 9d5f9b9df..000000000 --- a/data_structures/queue/queue.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "./queue.h" - -#include -#include - -/* Default constructor*/ -template -queue::queue() { - queueFront = NULL; - queueRear = NULL; - size = 0; -} - -/* Destructor */ -template -queue::~queue() {} - -/* Display for testing */ -template -void queue::display() { - node *current = queueFront; - std::cout << "Front --> "; - while (current != NULL) { - std::cout << current->data << " "; - current = current->next; - } - std::cout << std::endl; - std::cout << "Size of queue: " << size << std::endl; -} - -/* Determine whether the queue is empty */ -template -bool queue::isEmptyQueue() { - return (queueFront == NULL); -} - -/* Clear queue */ -template -void queue::clear() { - queueFront = NULL; -} - -/* Add new item to the queue */ -template -void queue::enQueue(Kind item) { - node *newNode; - newNode = new node; - newNode->data = item; - newNode->next = NULL; - if (queueFront == NULL) { - queueFront = newNode; - queueRear = newNode; - } else { - queueRear->next = newNode; - queueRear = queueRear->next; - } - size++; -} - -/* Return the top element of the queue */ -template -Kind queue::front() { - assert(queueFront != NULL); - return queueFront->data; -} - -/* Remove the element of the queue */ -template -void queue::deQueue() { - node *temp; - if (!isEmptyQueue()) { - temp = queueFront; - queueFront = queueFront->next; - delete temp; - size--; - } else { - 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 deleted file mode 100644 index d55ee3864..000000000 --- a/data_structures/queue/queue.h +++ /dev/null @@ -1,31 +0,0 @@ -/* This class specifies the basic operation on a queue as a linked list */ -#ifndef DATA_STRUCTURES_QUEUE_QUEUE_H_ -#define DATA_STRUCTURES_QUEUE_QUEUE_H_ - -/** Definition of the node */ -template -struct node { - Kind data; - node *next; -}; - -/** 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 clear(); - - private: - node *queueFront; /**< Pointer to the front of the queue */ - node *queueRear; /**< Pointer to the rear of the queue */ - int size; -}; - -#endif // DATA_STRUCTURES_QUEUE_QUEUE_H_ diff --git a/data_structures/stack.h b/data_structures/stack.h new file mode 100644 index 000000000..7b50e1f67 --- /dev/null +++ b/data_structures/stack.h @@ -0,0 +1,111 @@ +/* This class specifies the basic operation on a stack as a linked list */ +#ifndef DATA_STRUCTURES_STK_STACK_H_ +#define DATA_STRUCTURES_STK_STACK_H_ + +#include +#include + +/* Definition of the node */ +template +struct node { + Type data; + node *next; +}; + +/* Definition of the stack class */ +template +class stack { + public: + /** Show stack */ + void display() { + node *current = stackTop; + std::cout << "Top --> "; + while (current != NULL) { + std::cout << current->data << " "; + current = current->next; + } + std::cout << std::endl; + std::cout << "Size of stack: " << size << std::endl; + }; + + /** Default constructor*/ + stack() { + stackTop = NULL; + size = 0; + }; + + /** Destructor */ + ~stack(){}; + + /** Determine whether the stack is empty */ + bool isEmptyStack() { return (stackTop == NULL); }; + + /** Add new item to the stack */ + void push(Type item) { + node *newNode; + newNode = new node; + newNode->data = item; + newNode->next = stackTop; + stackTop = newNode; + size++; + }; + + /** Return the top element of the stack */ + Type top() { + assert(stackTop != NULL); + return stackTop->data; + }; + + /** Remove the top element of the stack */ + void pop() { + node *temp; + if (!isEmptyStack()) { + temp = stackTop; + stackTop = stackTop->next; + delete temp; + size--; + } else { + std::cout << "Stack is empty !" << std::endl; + } + }; + + /** Clear stack */ + void clear() { stackTop = NULL; }; + + /** Overload "=" the assignment operator */ + stack &operator=(const stack &otherStack) { + node *newNode, *current, *last; + + /* If stack is no empty, make it empty */ + if (stackTop != NULL) { + stackTop = NULL; + } + if (otherStack.stackTop == NULL) { + stackTop = NULL; + } else { + current = otherStack.stackTop; + stackTop = new node; + stackTop->data = current->data; + stackTop->next = NULL; + last = stackTop; + current = current->next; + /* Copy the remaining stack */ + while (current != NULL) { + newNode = new node; + newNode->data = current->data; + newNode->next = NULL; + last->next = newNode; + last = newNode; + current = current->next; + } + } + size = otherStack.size; + return *this; + }; + + private: + node *stackTop; /**< Pointer to the stack */ + int size; +}; + +#endif // DATA_STRUCTURES_STK_STACK_H_ diff --git a/data_structures/stk/CMakeLists.txt b/data_structures/stk/CMakeLists.txt deleted file mode 100644 index 30149ec06..000000000 --- a/data_structures/stk/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -add_library(libstack OBJECT stack.h stack.cpp) -add_executable( stack - main.cpp -) -target_link_libraries(stack PUBLIC libstack) -# set_target_properties(stack PROPERTIES LINKER_LANGUAGE CXX) - -add_executable( test_stack - test_stack.cpp -) -target_link_libraries(test_stack PUBLIC libstack) - -install(TARGETS stack DESTINATION "bin/data_structures") -install(TARGETS test_stack DESTINATION "bin/data_structures") -install(FILES students.txt DESTINATION "bin/data_structures") diff --git a/data_structures/stk/stack.cpp b/data_structures/stk/stack.cpp deleted file mode 100644 index 23aa3c05a..000000000 --- a/data_structures/stk/stack.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include "./stack.h" - -#include -#include - -/* Default constructor*/ -template -stack::stack() { - stackTop = NULL; - size = 0; -} - -/* Destructor */ -template -stack::~stack() {} - -/* Display for testing */ -template -void stack::display() { - node *current = stackTop; - std::cout << "Top --> "; - while (current != NULL) { - std::cout << current->data << " "; - current = current->next; - } - std::cout << std::endl; - std::cout << "Size of stack: " << size << std::endl; -} - -/* Determine whether the stack is empty */ -template -bool stack::isEmptyStack() { - return (stackTop == NULL); -} - -/* Clear stack */ -template -void stack::clear() { - stackTop = NULL; -} - -/* Add new item to the stack */ -template -void stack::push(Type item) { - node *newNode; - newNode = new node; - newNode->data = item; - newNode->next = stackTop; - stackTop = newNode; - size++; -} - -/* Return the top element of the stack */ -template -Type stack::top() { - assert(stackTop != NULL); - return stackTop->data; -} - -/* Remove the top element of the stack */ -template -void stack::pop() { - node *temp; - if (!isEmptyStack()) { - temp = stackTop; - stackTop = stackTop->next; - delete temp; - size--; - } else { - std::cout << "Stack is empty !" << std::endl; - } -} - -/* Operator "=" */ -template -stack &stack::operator=(const stack &otherStack) { - node *newNode, *current, *last; - - /* If stack is no empty, make it empty */ - if (stackTop != NULL) { - stackTop = NULL; - } - if (otherStack.stackTop == NULL) { - stackTop = NULL; - } else { - current = otherStack.stackTop; - stackTop = new node; - stackTop->data = current->data; - stackTop->next = NULL; - last = stackTop; - current = current->next; - /* Copy the remaining stack */ - while (current != NULL) { - newNode = new node; - newNode->data = current->data; - newNode->next = NULL; - last->next = newNode; - last = newNode; - current = current->next; - } - } - size = otherStack.size; - return *this; -} - -#include -/** force instantiate to export the type class */ -template class stack; -template class stack; diff --git a/data_structures/stk/stack.h b/data_structures/stk/stack.h deleted file mode 100644 index e038440a1..000000000 --- a/data_structures/stk/stack.h +++ /dev/null @@ -1,32 +0,0 @@ -/* This class specifies the basic operation on a stack as a linked list */ -#ifndef DATA_STRUCTURES_STK_STACK_H_ -#define DATA_STRUCTURES_STK_STACK_H_ - -/* Definition of the node */ -template -struct node { - Type data; - node *next; -}; - -/* Definition of the stack class */ -template -class stack { - public: - void display(); /* Show stack */ - stack(); /* Default constructor*/ - ~stack(); /* Destructor */ - bool isEmptyStack(); /* Determine whether the stack is empty */ - void push(Type item); /* Add new item to the stack */ - Type top(); /* Return the top element of the stack */ - void pop(); /* Remove the top element of the stack */ - void clear(); - - stack &operator=(const stack &otherStack); - // Overload "=" the assignment operator. - private: - node *stackTop; /* Pointer to the stack */ - int size; -}; - -#endif // DATA_STRUCTURES_STK_STACK_H_ diff --git a/data_structures/stk/student.txt b/data_structures/student.txt similarity index 100% rename from data_structures/stk/student.txt rename to data_structures/student.txt diff --git a/data_structures/queue/test_queue.cpp b/data_structures/test_queue.cpp similarity index 100% rename from data_structures/queue/test_queue.cpp rename to data_structures/test_queue.cpp diff --git a/data_structures/stk/test_stack.cpp b/data_structures/test_stack.cpp similarity index 100% rename from data_structures/stk/test_stack.cpp rename to data_structures/test_stack.cpp diff --git a/data_structures/stk/main.cpp b/data_structures/test_stack_students.cpp similarity index 100% rename from data_structures/stk/main.cpp rename to data_structures/test_stack_students.cpp