From 4bb4e8a8322a1f9ad3036bd4f855ec1ab6626ada Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 23 Jun 2021 07:03:49 +0000 Subject: [PATCH] clang-format and clang-tidy fixes for e59bc3bf --- search/sublist_search.cpp | 86 +++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/search/sublist_search.cpp b/search/sublist_search.cpp index fc932ca55..4062e9810 100644 --- a/search/sublist_search.cpp +++ b/search/sublist_search.cpp @@ -1,25 +1,31 @@ /** * @file - * @brief Implementation of the [Sublist Search Algorithm](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list) algorithm. + * @brief Implementation of the [Sublist Search + * Algorithm](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list) + * algorithm. * @details - * * Sublist search is used to detect a presence of one list in another list. - * * 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 list), then we can perform the sublist search to find it. + * * Sublist search is used to detect a presence of one list in another + * list. + * * 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 + * list), then we can perform the sublist search to find it. * - * * For instance, the first list contains these elements: 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. + * * For instance, the first list contains these elements: 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. * * ### Working - * * The sublist search algorithm works by comparing the first element of the first list with the - * first element of the second list. - * * 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. + * * The sublist search algorithm works by comparing the first element + * of the first list with the first element of the second list. + * * 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) */ +#include // For assert #include // for IO operations -#include // For passing data as array -#include // For assert +#include // For passing data as array /** * A Node structure representing single link Node in a linked list. @@ -43,14 +49,16 @@ bool sublistSearch(Node *sublist, Node *mainList) { Node *target_ptr = sublist; while (mainList != nullptr) { - Node *main_ptr = mainList; // Initialize main pointer to the current node of main list. + Node *main_ptr = mainList; // Initialize main pointer to the current + // node of main list. while (target_ptr != nullptr) { if (main_ptr == nullptr) { return false; } else if (main_ptr->data == target_ptr->data) { - // If the data of target node and main node is equal then move to the next node of both lists. + // If the data of target node and main node is equal then move + // to the next node of both lists. target_ptr = target_ptr->next; main_ptr = main_ptr->next; @@ -60,13 +68,16 @@ bool sublistSearch(Node *sublist, Node *mainList) { } if (target_ptr == nullptr) { - // Is target pointer becomes null that means the target list is been traversed without returning false. - // Which means the sublist has been found and return ture. + // Is target pointer becomes null that means the target list is been + // traversed without returning false. Which means the sublist has + // been found and return ture. return true; } - target_ptr = sublist; // set the target pointer again to stating point of target list. - mainList = mainList->next; // set the main pointer to the next element of the main list and repeat the algo. + target_ptr = sublist; // set the target pointer again to stating point + // of target list. + mainList = mainList->next; // set the main pointer to the next element + // of the main list and repeat the algo. } // If the main list is exhausted, means sublist does not found, return false @@ -88,7 +99,8 @@ void printLinkedList(Node *start) { /** * @brief Makes a dummy linked list for testing. - * @param data A vector of "int" containing the data that is supposed to be stored in nodes of linked list. + * @param data A vector of "int" containing the data that is supposed to be + * stored in nodes of linked list. * @returns Node* A head pointer to the linked list. */ Node *makeLinkedList(const std::vector &data) { @@ -101,8 +113,7 @@ Node *makeLinkedList(const std::vector &data) { if (head == nullptr) { head = node; tail = node; - } - else { + } else { tail->next = node; tail = tail->next; } @@ -114,14 +125,14 @@ Node *makeLinkedList(const std::vector &data) { * A class, encapsulating the necessary test cases. */ class TestCases { -private: - template + private: + template void log(T msg) { // It's just to avoid writing cout and endl std::cout << "[TESTS] : ---> " << msg << std::endl; } -public: + public: void runTests() { log("Running Tests..."); @@ -136,7 +147,8 @@ public: void testCase_1() { const bool expectedOutput = true; - log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "~"); log("This is test case 1 for sublist search Algorithm : "); log("Description:"); log(" EDGE CASE : Only contains one element"); @@ -154,17 +166,18 @@ public: log("Assertion check passed!"); log("[PASS] : TEST CASE 1 PASS!"); - log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); - - delete(sublistLL); - delete(mainlistLL); + log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "~"); + delete (sublistLL); + delete (mainlistLL); } void testCase_2() { const bool expectedOutput = true; - log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "~"); log("This is test case 2 for sublist search Algorithm : "); log("Description:"); log(" contains main list of 100 elements and sublist of 20"); @@ -194,13 +207,15 @@ public: log("Assertion check passed!"); log("[PASS] : TEST CASE 2 PASS!"); - log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "~"); } void testCase_3() { const bool expectedOutput = false; - log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "~"); log("This is test case 3 for sublist search Algorithm : "); log("Description:"); log(" contains main list of 50 elements and sublist of 20"); @@ -208,7 +223,6 @@ public: std::vector sublistData(20); std::vector mainlistData(50); - for (int i = 0; i < 50; i++) { // Inserts 100 elements in main list mainlistData.push_back(i + 1); @@ -229,7 +243,8 @@ public: log("Assertion check passed!"); log("[PASS] : TEST CASE 3 PASS!"); - log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "~"); } }; @@ -265,10 +280,9 @@ int main(int argc, char *argv[]) { printLinkedList(mainlistLL); std::cout << std::endl; - if (exists){ + if (exists) { std::cout << "[TRUE] - sublist found in main list"; - } - else{ + } else { std::cout << "[FALSE] - sublist NOT found in main list"; } return 0;