mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-06-16 07:09:35 +08:00
template header files contain function codes as well and removed redundant subfolders
Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
@@ -18,5 +18,3 @@ foreach( testsourcefile ${APP_SOURCES} )
|
||||
endforeach( testsourcefile ${APP_SOURCES} )
|
||||
|
||||
add_subdirectory(cll)
|
||||
add_subdirectory(queue)
|
||||
add_subdirectory(stk)
|
||||
|
||||
88
data_structures/queue.h
Normal file
88
data_structures/queue.h
Normal file
@@ -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 <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/** Definition of the node */
|
||||
template <class Kind>
|
||||
struct node {
|
||||
Kind data;
|
||||
node<Kind> *next;
|
||||
};
|
||||
|
||||
/** Definition of the queue class */
|
||||
template <class Kind>
|
||||
class queue {
|
||||
public:
|
||||
/** Show queue */
|
||||
void display() {
|
||||
node<Kind> *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<Kind> *newNode;
|
||||
newNode = new node<Kind>;
|
||||
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<Kind> *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<Kind> *queueFront; /**< Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /**< Pointer to the rear of the queue */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif // DATA_STRUCTURES_QUEUE_QUEUE_H_
|
||||
@@ -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")
|
||||
@@ -1,83 +0,0 @@
|
||||
#include "./queue.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Kind>
|
||||
queue<Kind>::queue() {
|
||||
queueFront = NULL;
|
||||
queueRear = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
template <class Kind>
|
||||
queue<Kind>::~queue() {}
|
||||
|
||||
/* Display for testing */
|
||||
template <class Kind>
|
||||
void queue<Kind>::display() {
|
||||
node<Kind> *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 <class Kind>
|
||||
bool queue<Kind>::isEmptyQueue() {
|
||||
return (queueFront == NULL);
|
||||
}
|
||||
|
||||
/* Clear queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::clear() {
|
||||
queueFront = NULL;
|
||||
}
|
||||
|
||||
/* Add new item to the queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::enQueue(Kind item) {
|
||||
node<Kind> *newNode;
|
||||
newNode = new node<Kind>;
|
||||
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 <class Kind>
|
||||
Kind queue<Kind>::front() {
|
||||
assert(queueFront != NULL);
|
||||
return queueFront->data;
|
||||
}
|
||||
|
||||
/* Remove the element of the queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::deQueue() {
|
||||
node<Kind> *temp;
|
||||
if (!isEmptyQueue()) {
|
||||
temp = queueFront;
|
||||
queueFront = queueFront->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
std::cout << "Queue is empty !" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#include <string>
|
||||
/** force instantiate to export the type class */
|
||||
template class queue<std::string>;
|
||||
@@ -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 <class Kind>
|
||||
struct node {
|
||||
Kind data;
|
||||
node<Kind> *next;
|
||||
};
|
||||
|
||||
/** Definition of the queue class */
|
||||
template <class Kind>
|
||||
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<Kind> *queueFront; /**< Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /**< Pointer to the rear of the queue */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif // DATA_STRUCTURES_QUEUE_QUEUE_H_
|
||||
111
data_structures/stack.h
Normal file
111
data_structures/stack.h
Normal file
@@ -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 <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Type>
|
||||
struct node {
|
||||
Type data;
|
||||
node<Type> *next;
|
||||
};
|
||||
|
||||
/* Definition of the stack class */
|
||||
template <class Type>
|
||||
class stack {
|
||||
public:
|
||||
/** Show stack */
|
||||
void display() {
|
||||
node<Type> *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<Type> *newNode;
|
||||
newNode = new node<Type>;
|
||||
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<Type> *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<Type> &operator=(const stack<Type> &otherStack) {
|
||||
node<Type> *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<Type>;
|
||||
stackTop->data = current->data;
|
||||
stackTop->next = NULL;
|
||||
last = stackTop;
|
||||
current = current->next;
|
||||
/* Copy the remaining stack */
|
||||
while (current != NULL) {
|
||||
newNode = new node<Type>;
|
||||
newNode->data = current->data;
|
||||
newNode->next = NULL;
|
||||
last->next = newNode;
|
||||
last = newNode;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
size = otherStack.size;
|
||||
return *this;
|
||||
};
|
||||
|
||||
private:
|
||||
node<Type> *stackTop; /**< Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif // DATA_STRUCTURES_STK_STACK_H_
|
||||
@@ -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")
|
||||
@@ -1,109 +0,0 @@
|
||||
#include "./stack.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Type>
|
||||
stack<Type>::stack() {
|
||||
stackTop = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
template <class Type>
|
||||
stack<Type>::~stack() {}
|
||||
|
||||
/* Display for testing */
|
||||
template <class Type>
|
||||
void stack<Type>::display() {
|
||||
node<Type> *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 <class Type>
|
||||
bool stack<Type>::isEmptyStack() {
|
||||
return (stackTop == NULL);
|
||||
}
|
||||
|
||||
/* Clear stack */
|
||||
template <class Type>
|
||||
void stack<Type>::clear() {
|
||||
stackTop = NULL;
|
||||
}
|
||||
|
||||
/* Add new item to the stack */
|
||||
template <class Type>
|
||||
void stack<Type>::push(Type item) {
|
||||
node<Type> *newNode;
|
||||
newNode = new node<Type>;
|
||||
newNode->data = item;
|
||||
newNode->next = stackTop;
|
||||
stackTop = newNode;
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Return the top element of the stack */
|
||||
template <class Type>
|
||||
Type stack<Type>::top() {
|
||||
assert(stackTop != NULL);
|
||||
return stackTop->data;
|
||||
}
|
||||
|
||||
/* Remove the top element of the stack */
|
||||
template <class Type>
|
||||
void stack<Type>::pop() {
|
||||
node<Type> *temp;
|
||||
if (!isEmptyStack()) {
|
||||
temp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
std::cout << "Stack is empty !" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/* Operator "=" */
|
||||
template <class Type>
|
||||
stack<Type> &stack<Type>::operator=(const stack<Type> &otherStack) {
|
||||
node<Type> *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<Type>;
|
||||
stackTop->data = current->data;
|
||||
stackTop->next = NULL;
|
||||
last = stackTop;
|
||||
current = current->next;
|
||||
/* Copy the remaining stack */
|
||||
while (current != NULL) {
|
||||
newNode = new node<Type>;
|
||||
newNode->data = current->data;
|
||||
newNode->next = NULL;
|
||||
last->next = newNode;
|
||||
last = newNode;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
size = otherStack.size;
|
||||
return *this;
|
||||
}
|
||||
|
||||
#include <string>
|
||||
/** force instantiate to export the type class */
|
||||
template class stack<std::string>;
|
||||
template class stack<int>;
|
||||
@@ -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 <class Type>
|
||||
struct node {
|
||||
Type data;
|
||||
node<Type> *next;
|
||||
};
|
||||
|
||||
/* Definition of the stack class */
|
||||
template <class Type>
|
||||
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<Type> &operator=(const stack<Type> &otherStack);
|
||||
// Overload "=" the assignment operator.
|
||||
private:
|
||||
node<Type> *stackTop; /* Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif // DATA_STRUCTURES_STK_STACK_H_
|
||||
Reference in New Issue
Block a user