Skip to content

Commit f6913b7

Browse files
committed
added one liner docs
1 parent e7b332e commit f6913b7

File tree

1 file changed

+93
-112
lines changed

1 file changed

+93
-112
lines changed

search/sublist_search.cpp

Lines changed: 93 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -39,55 +39,54 @@ namespace search {
3939
* Search](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list)
4040
* implementation
4141
*/
42-
namespace sublist_search {
42+
namespace sublist_search {
4343
/**
4444
* @brief A Node structure representing a single link Node in a linked list
4545
*/
46-
struct Node {
47-
uint32_t data = 0; ///< the key/value of the node
48-
Node *next{}; ///< pointer to the next node
49-
};
46+
struct Node {
47+
uint32_t data = 0; ///< the key/value of the node
48+
Node *next{}; ///< pointer to the next node
49+
};
5050

5151
/**
5252
* @brief A simple function to print the linked list
5353
* @param start The head of the linked list
5454
* @returns void
5555
*/
56-
void printLinkedList(Node *start) {
57-
while (start != nullptr) {
58-
std::cout << "->" << start->data;
59-
start = start->next;
60-
}
61-
std::cout << std::endl;
62-
}
56+
void printLinkedList(Node *start) {
57+
while (start != nullptr) {
58+
std::cout << "->" << start->data;
59+
start = start->next;
60+
}
61+
std::cout << std::endl;
62+
}
6363

6464
/**
6565
* @brief Give a vector of data,
66-
* it adds each element of vector in the linked list and return the address of
67-
* head pointer.
66+
* it adds each element of vector in the linked list and return the address of head pointer.
6867
* @param data A vector of "int" containing the data that is supposed to be
6968
* stored in nodes of linked list.
7069
* @returns Node* A head pointer to the linked list.
7170
*/
72-
Node *makeLinkedList(const std::vector<uint64_t> &data) {
73-
/// This is used in test cases for rapidly creating linked list with 100+
74-
/// elements, instead of hard-coding 100 elements in test cases.
75-
Node *head = nullptr;
76-
Node *tail = nullptr;
77-
for (int i : data) {
78-
Node *node = new Node;
79-
node->data = i;
80-
node->next = nullptr;
81-
if (head == nullptr) {
82-
head = node;
83-
tail = node;
84-
} else {
85-
tail->next = node;
86-
tail = tail->next;
71+
Node *makeLinkedList(const std::vector<uint64_t> &data) {
72+
/// This is used in test cases for rapidly creating linked list with 100+ elements, instead of hard-coding
73+
/// 100 elements in test cases.
74+
Node *head = nullptr;
75+
Node *tail = nullptr;
76+
for (int i : data) {
77+
Node *node = new Node;
78+
node->data = i;
79+
node->next = nullptr;
80+
if (head == nullptr) {
81+
head = node;
82+
tail = node;
83+
} else {
84+
tail->next = node;
85+
tail = tail->next;
86+
}
87+
}
88+
return head;
8789
}
88-
}
89-
return head;
90-
}
9190

9291
/**
9392
* @brief Main searching function
@@ -96,61 +95,59 @@ Node *makeLinkedList(const std::vector<uint64_t> &data) {
9695
* @returns true if the sublist is found
9796
* @returns false if the sublist is NOT found
9897
*/
99-
bool sublistSearch(Node *sublist, Node *mainList) {
100-
if (sublist == nullptr || mainList == nullptr) {
101-
return false;
102-
}
98+
bool sublistSearch(Node *sublist, Node *mainList) {
99+
if (sublist == nullptr || mainList == nullptr) {
100+
return false;
101+
}
103102

104-
/// Initialize target pointer to the head node of sublist.
105-
Node *target_ptr = sublist;
103+
/// Initialize target pointer to the head node of sublist.
104+
Node *target_ptr = sublist;
106105

107-
while (mainList != nullptr) {
108-
/// Initialize main pointer to the current node of main list.
109-
Node *main_ptr = mainList;
106+
while (mainList != nullptr) {
107+
/// Initialize main pointer to the current node of main list.
108+
Node *main_ptr = mainList;
110109

111-
while (target_ptr != nullptr) {
112-
if (main_ptr == nullptr) {
113-
return false;
110+
while (target_ptr != nullptr) {
111+
if (main_ptr == nullptr) {
112+
return false;
114113

115-
} else if (main_ptr->data == target_ptr->data) {
116-
/// If the data of target node and main node is equal then move
117-
/// to the next node of both lists.
118-
target_ptr = target_ptr->next;
119-
main_ptr = main_ptr->next;
114+
} else if (main_ptr->data == target_ptr->data) {
115+
/// If the data of target node and main node is equal then move
116+
/// to the next node of both lists.
117+
target_ptr = target_ptr->next;
118+
main_ptr = main_ptr->next;
120119

121-
} else {
122-
break;
123-
}
124-
}
120+
} else {
121+
break;
122+
}
123+
}
125124

126-
if (target_ptr == nullptr) {
127-
/// Is target pointer becomes null that means the target list is
128-
/// been traversed without returning false. Which means the sublist
129-
/// has been found and return ture.
130-
return true;
131-
}
125+
if (target_ptr == nullptr) {
126+
/// Is target pointer becomes null that means the target list is been
127+
/// traversed without returning false. Which means the sublist has
128+
/// been found and return ture.
129+
return true;
130+
}
132131

133-
/// set the target pointer again to stating point of target list.
134-
target_ptr = sublist;
132+
/// set the target pointer again to stating point of target list.
133+
target_ptr = sublist;
135134

136-
/// set the main pointer to the next element of the main list and repeat
137-
/// the algo.
138-
mainList = mainList->next;
139-
}
135+
/// set the main pointer to the next element of the main list and repeat the algo.
136+
mainList = mainList->next;
137+
}
140138

141-
/// If the main list is exhausted, means sublist does not found, return
142-
/// false
143-
return false;
144-
}
139+
/// If the main list is exhausted, means sublist does not found, return false
140+
return false;
141+
}
145142

146-
} // namespace sublist_search
143+
} // namespace sublist_search
147144
} // namespace search
148145

149146
/**
150147
* @brief class encapsulating the necessary test cases
151148
*/
152149
class TestCases {
153-
private:
150+
private:
154151
/**
155152
* @brief A function to print given message on console.
156153
* @tparam T Type of the given message.
@@ -162,7 +159,7 @@ class TestCases {
162159
std::cout << "[TESTS] : ---> " << msg << std::endl;
163160
}
164161

165-
public:
162+
public:
166163
/**
167164
* @brief Executes test cases
168165
* @returns void
@@ -183,29 +180,24 @@ class TestCases {
183180
* @returns void
184181
* */
185182
void testCase_1() {
186-
const bool expectedOutput = true; /// Expected output of this test
183+
const bool expectedOutput = true; ///< Expected output of this test
187184

188185
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
189186
"~");
190187
log("This is test case 1 for sublist search Algorithm : ");
191188
log("Description:");
192189
log(" EDGE CASE : Only contains one element");
193190

194-
std::vector<uint64_t> sublistData = {
195-
6}; /// Data to make linked list which will be the sublist
196-
std::vector<uint64_t> mainlistData = {
197-
2, 5, 6, 7,
198-
8}; /// Data to make linked list which will be the main list
191+
std::vector<uint64_t> sublistData = {6}; ///< Data to make linked list which will be the sublist
192+
std::vector<uint64_t> mainlistData = {2, 5, 6, 7, 8}; ///< Data to make linked list which will be the main list
199193

200194
search::sublist_search::Node *sublistLL =
201-
search::sublist_search::makeLinkedList(
202-
sublistData); /// Sublist to be searched
195+
search::sublist_search::makeLinkedList(sublistData); ///< Sublist to be searched
203196
search::sublist_search::Node *mainlistLL =
204-
search::sublist_search::makeLinkedList(
205-
mainlistData); /// Main list in which sublist is to be searched
197+
search::sublist_search::makeLinkedList(mainlistData); ///< Main list in which sublist is to be searched
206198

207199
bool exists = search::sublist_search::sublistSearch(
208-
sublistLL, mainlistLL); /// boolean, if sublist exist or not
200+
sublistLL, mainlistLL); ///< boolean, if sublist exist or not
209201

210202
log("Checking assert expression...");
211203
assert(exists == expectedOutput);
@@ -227,16 +219,13 @@ class TestCases {
227219
void testCase_2() {
228220
const bool expectedOutput = true; /// Expected output of this test
229221

230-
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
231-
"~");
222+
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
232223
log("This is test case 2 for sublist search Algorithm : ");
233224
log("Description:");
234225
log(" contains main list of 100 elements and sublist of 20");
235226

236-
std::vector<uint64_t> sublistData(
237-
20); /// Data to make linked list which will be the sublist
238-
std::vector<uint64_t> mainlistData(
239-
100); /// Main list in which sublist is to be searched
227+
std::vector<uint64_t> sublistData(20); ///< Data to make linked list which will be the sublist
228+
std::vector<uint64_t> mainlistData(100); ///< Main list in which sublist is to be searched
240229

241230
for (int i = 0; i < 100; i++) {
242231
/// Inserts 100 elements in main list
@@ -251,22 +240,19 @@ class TestCases {
251240
}
252241

253242
search::sublist_search::Node *sublistLL =
254-
search::sublist_search::makeLinkedList(
255-
sublistData); /// Sublist to be searched
243+
search::sublist_search::makeLinkedList(sublistData); ///< Sublist to be searched
256244
search::sublist_search::Node *mainlistLL =
257-
search::sublist_search::makeLinkedList(
258-
mainlistData); /// Main list in which sublist is to be searched
245+
search::sublist_search::makeLinkedList(mainlistData); ///< Main list in which sublist is to be searched
259246

260247
bool exists = search::sublist_search::sublistSearch(
261-
sublistLL, mainlistLL); // boolean, if sublist exist or not
248+
sublistLL, mainlistLL); ///< boolean, if sublist exist or not
262249

263250
log("Checking assert expression...");
264251
assert(exists == expectedOutput);
265252
log("Assertion check passed!");
266253

267254
log("[PASS] : TEST CASE 2 PASS!");
268-
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
269-
"~");
255+
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
270256
}
271257

272258
/**
@@ -275,17 +261,15 @@ class TestCases {
275261
* @returns void
276262
* */
277263
void testCase_3() {
278-
const bool expectedOutput = false; /// Expected output of this test
264+
const bool expectedOutput = false; ///< Expected output of this test
279265

280-
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
281-
"~");
266+
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
282267
log("This is test case 3 for sublist search Algorithm : ");
283268
log("Description:");
284269
log(" contains main list of 50 elements and sublist of 20");
285270

286-
std::vector<uint64_t> sublistData(20); /// Sublist to be searched
287-
std::vector<uint64_t> mainlistData(
288-
50); /// Main list in which sublist is to be searched
271+
std::vector<uint64_t> sublistData(20); ///< Sublist to be searched
272+
std::vector<uint64_t> mainlistData(50); ///< Main list in which sublist is to be searched
289273

290274
for (int i = 0; i < 50; i++) {
291275
/// Inserts 100 elements in main list
@@ -298,22 +282,19 @@ class TestCases {
298282
}
299283

300284
search::sublist_search::Node *sublistLL =
301-
search::sublist_search::makeLinkedList(
302-
sublistData); /// Sublist to be searched
285+
search::sublist_search::makeLinkedList(sublistData); ///< Sublist to be searched
303286
search::sublist_search::Node *mainlistLL =
304-
search::sublist_search::makeLinkedList(
305-
mainlistData); /// Main list in which sublist is to be searched
287+
search::sublist_search::makeLinkedList(mainlistData); ///< Main list in which sublist is to be searched
306288

307289
bool exists = search::sublist_search::sublistSearch(
308-
sublistLL, mainlistLL); /// boolean, if sublist exist or not
290+
sublistLL, mainlistLL); ///< boolean, if sublist exist or not
309291

310292
log("Checking assert expression...");
311293
assert(exists == expectedOutput);
312294
log("Assertion check passed!");
313295

314296
log("[PASS] : TEST CASE 3 PASS!");
315-
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
316-
"~");
297+
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
317298
}
318299
};
319300

@@ -339,12 +320,12 @@ int main(int argc, char *argv[]) {
339320
std::vector<uint64_t> sublistData = {6, 8};
340321

341322
search::sublist_search::Node *mainlistLL =
342-
search::sublist_search::makeLinkedList(mainlistData);
323+
search::sublist_search::makeLinkedList(mainlistData);
343324
search::sublist_search::Node *sublistLL =
344-
search::sublist_search::makeLinkedList(sublistData);
325+
search::sublist_search::makeLinkedList(sublistData);
345326

346327
bool exists = search::sublist_search::sublistSearch(
347-
sublistLL, mainlistLL); // boolean, if sublist exist or not
328+
sublistLL, mainlistLL); // boolean, if sublist exist or not
348329

349330
std::cout << "Sublist :" << std::endl;
350331
search::sublist_search::printLinkedList(sublistLL);

0 commit comments

Comments
 (0)