mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-12 06:56:59 +08:00
clang-format and clang-tidy fixes for e59bc3bf
This commit is contained in:
@@ -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 <cassert> // For assert
|
||||
#include <iostream> // for IO operations
|
||||
#include <vector> // For passing data as array
|
||||
#include <cassert> // For assert
|
||||
#include <vector> // 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<int> &data) {
|
||||
@@ -101,8 +113,7 @@ Node *makeLinkedList(const std::vector<int> &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<int> &data) {
|
||||
* A class, encapsulating the necessary test cases.
|
||||
*/
|
||||
class TestCases {
|
||||
private:
|
||||
template<typename T>
|
||||
private:
|
||||
template <typename T>
|
||||
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<int> sublistData(20);
|
||||
std::vector<int> 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;
|
||||
|
||||
Reference in New Issue
Block a user