[feat/fix/docs]: Perform some necessary changes

This commit is contained in:
Panquesito7
2021-07-02 23:15:27 +00:00
parent 4247d4a005
commit 5a02b336d7

View File

@@ -3,21 +3,21 @@
* @brief Implementation of the [Sublist Search * @brief Implementation of the [Sublist Search
* Algorithm](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list) * Algorithm](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list)
* @details * @details
* * Sublist search is used to detect a presence of one list in another * * Sublist search is used to detect a presence of one list in another list.
* list. * * Suppose we have a single-node list (let's say the first list), and we
* * Suppose we have a single-node list (let's say the first list), and we * want to ensure that the list is present in another list (let's say the second
* want to ensure that the list is present in another list (let's say the second * list), then we can perform the sublist search to find it.
* list), then we can perform the sublist search to find it.
* *
* * For instance, the first list contains these elements: 23 -> 30 -> 41, * * For instance, the first list contains these elements: 23 -> 30 -> 41,
* and the second list contains these elements: 10 -> 15 -> 23 -> 30 -> 41 * and the second list contains these elements: 10 -> 15 -> 23 -> 30 -> 41
* -> 49. At a glance, we see that the first list presents in the second list. * -> 49. At a glance, we see that the first list presents in the second list.
* *
* ### Working * ### Working
* * The sublist search algorithm works by comparing the first element *
* of the first list with the first element of the second list. * * The sublist search algorithm works by comparing the first element
* * If the two values don't match, it goes to the next element of the * of the first list with the first element of the second list.
* second list. It does this until the two values match. * * If the two values don't match, it goes to the next element of the
* second list. It does this until the two values match.
* *
* @author [Nitin Sharma](https://github.com/foo290) * @author [Nitin Sharma](https://github.com/foo290)
*/ */
@@ -27,10 +27,20 @@
#include <vector> /// for std::vector #include <vector> /// for std::vector
/** /**
* @brief A Node structure representing a single link Node in a linked list. * @namespace search
* @brief Searching algorithms
*/
namespace search {
/**
* @namespace sublist_search
* @brief Functions for the [Sublist Search](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list) implementation
*/
namespace sublist_search {
/**
* @brief A Node structure representing a single link Node in a linked list
*/ */
struct Node { struct Node {
int data; uint32_t data = 0;
Node *next; Node *next;
}; };
@@ -103,7 +113,7 @@ void printLinkedList(Node *start) {
* stored in nodes of linked list. * stored in nodes of linked list.
* @returns Node* A head pointer to the linked list. * @returns Node* A head pointer to the linked list.
*/ */
Node *makeLinkedList(const std::vector<int> &data) { Node *makeLinkedList(const std::vector<uint64_t> &data) {
Node *head = nullptr; Node *head = nullptr;
Node *tail = nullptr; Node *tail = nullptr;
for (int i : data) { for (int i : data) {
@@ -120,9 +130,11 @@ Node *makeLinkedList(const std::vector<int> &data) {
} }
return head; return head;
} }
} // namespace sublist_search
} // namespace search
/** /**
* A class, encapsulating the necessary test cases. * @brief class encapsulating the necessary test cases
*/ */
class TestCases { class TestCases {
private: private:
@@ -153,13 +165,13 @@ class TestCases {
log("Description:"); log("Description:");
log(" EDGE CASE : Only contains one element"); log(" EDGE CASE : Only contains one element");
std::vector<int> sublistData = {6}; std::vector<uint64_t> sublistData = {6};
std::vector<int> mainlistData = {2, 5, 6, 7, 8}; std::vector<uint64_t> mainlistData = {2, 5, 6, 7, 8};
Node *sublistLL = makeLinkedList(sublistData); search::sublist_search::Node *sublistLL = search::sublist_search::makeLinkedList(sublistData);
Node *mainlistLL = makeLinkedList(mainlistData); search::sublist_search::Node *mainlistLL = search::sublist_search::makeLinkedList(mainlistData);
bool exists = sublistSearch(sublistLL, mainlistLL); bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
log("Checking assert expression..."); log("Checking assert expression...");
assert(exists == expectedOutput); assert(exists == expectedOutput);
@@ -182,8 +194,8 @@ class TestCases {
log("Description:"); log("Description:");
log(" contains main list of 100 elements and sublist of 20"); log(" contains main list of 100 elements and sublist of 20");
std::vector<int> sublistData(20); std::vector<uint64_t> sublistData(20);
std::vector<int> mainlistData(100); std::vector<uint64_t> mainlistData(100);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
// Inserts 100 elements in main list // Inserts 100 elements in main list
@@ -197,10 +209,10 @@ class TestCases {
temp++; temp++;
} }
Node *sublistLL = makeLinkedList(sublistData); search::sublist_search::Node *sublistLL = search::sublist_search::makeLinkedList(sublistData);
Node *mainlistLL = makeLinkedList(mainlistData); search::sublist_search::Node *mainlistLL = search::sublist_search::makeLinkedList(mainlistData);
bool exists = sublistSearch(sublistLL, mainlistLL); bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
log("Checking assert expression..."); log("Checking assert expression...");
assert(exists == expectedOutput); assert(exists == expectedOutput);
@@ -220,8 +232,8 @@ class TestCases {
log("Description:"); log("Description:");
log(" contains main list of 50 elements and sublist of 20"); log(" contains main list of 50 elements and sublist of 20");
std::vector<int> sublistData(20); std::vector<uint64_t> sublistData(20);
std::vector<int> mainlistData(50); std::vector<uint64_t> mainlistData(50);
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
// Inserts 100 elements in main list // Inserts 100 elements in main list
@@ -233,10 +245,10 @@ class TestCases {
sublistData.push_back(i + 1); sublistData.push_back(i + 1);
} }
Node *sublistLL = makeLinkedList(sublistData); search::sublist_search::Node *sublistLL = search::sublist_search::makeLinkedList(sublistData);
Node *mainlistLL = makeLinkedList(mainlistData); search::sublist_search::Node *mainlistLL = search::sublist_search::makeLinkedList(mainlistData);
bool exists = sublistSearch(sublistLL, mainlistLL); bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
log("Checking assert expression..."); log("Checking assert expression...");
assert(exists == expectedOutput); assert(exists == expectedOutput);
@@ -264,26 +276,26 @@ static void test() {
* @returns 0 on exit * @returns 0 on exit
*/ */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
test(); // Executing various test cases first. test(); // run self-test implementations
std::vector<int> mainlistData = {2, 5, 6, 7, 8}; std::vector<uint64_t> mainlistData = {2, 5, 6, 7, 8};
std::vector<int> sublistData = {6, 8}; std::vector<uint64_t> sublistData = {6, 8};
Node *mainlistLL = makeLinkedList(mainlistData); search::sublist_search::Node *mainlistLL = search::sublist_search::makeLinkedList(mainlistData);
Node *sublistLL = makeLinkedList(sublistData); search::sublist_search::Node *sublistLL = search::sublist_search::makeLinkedList(sublistData);
bool exists = sublistSearch(sublistLL, mainlistLL); bool exists = search::sublist_search::sublistSearch(sublistLL, mainlistLL);
std::cout << "Sublist :" << std::endl; std::cout << "Sublist :" << std::endl;
printLinkedList(sublistLL); search::sublist_search::printLinkedList(sublistLL);
std::cout << "Main list : " << std::endl; std::cout << "Main list : " << std::endl;
printLinkedList(mainlistLL); search::sublist_search::printLinkedList(mainlistLL);
std::cout << std::endl; std::cout << std::endl;
if (exists) { if (exists) {
std::cout << "[TRUE] - sublist found in main list"; std::cout << "[TRUE] - sublist found in main list\n";
} else { } else {
std::cout << "[FALSE] - sublist NOT found in main list"; std::cout << "[FALSE] - sublist NOT found in main list\n";
} }
return 0; return 0;
} }