@@ -39,55 +39,54 @@ namespace search {
39
39
* Search](https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list)
40
40
* implementation
41
41
*/
42
- namespace sublist_search {
42
+ namespace sublist_search {
43
43
/* *
44
44
* @brief A Node structure representing a single link Node in a linked list
45
45
*/
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
+ };
50
50
51
51
/* *
52
52
* @brief A simple function to print the linked list
53
53
* @param start The head of the linked list
54
54
* @returns void
55
55
*/
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
+ }
63
63
64
64
/* *
65
65
* @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.
68
67
* @param data A vector of "int" containing the data that is supposed to be
69
68
* stored in nodes of linked list.
70
69
* @returns Node* A head pointer to the linked list.
71
70
*/
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;
87
89
}
88
- }
89
- return head;
90
- }
91
90
92
91
/* *
93
92
* @brief Main searching function
@@ -96,61 +95,59 @@ Node *makeLinkedList(const std::vector<uint64_t> &data) {
96
95
* @returns true if the sublist is found
97
96
* @returns false if the sublist is NOT found
98
97
*/
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
+ }
103
102
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;
106
105
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;
110
109
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 ;
114
113
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 ;
120
119
121
- } else {
122
- break ;
123
- }
124
- }
120
+ } else {
121
+ break ;
122
+ }
123
+ }
125
124
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
+ }
132
131
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;
135
134
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
+ }
140
138
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
+ }
145
142
146
- } // namespace sublist_search
143
+ } // namespace sublist_search
147
144
} // namespace search
148
145
149
146
/* *
150
147
* @brief class encapsulating the necessary test cases
151
148
*/
152
149
class TestCases {
153
- private:
150
+ private:
154
151
/* *
155
152
* @brief A function to print given message on console.
156
153
* @tparam T Type of the given message.
@@ -162,7 +159,7 @@ class TestCases {
162
159
std::cout << " [TESTS] : ---> " << msg << std::endl;
163
160
}
164
161
165
- public:
162
+ public:
166
163
/* *
167
164
* @brief Executes test cases
168
165
* @returns void
@@ -183,29 +180,24 @@ class TestCases {
183
180
* @returns void
184
181
* */
185
182
void testCase_1 () {
186
- const bool expectedOutput = true ; // / Expected output of this test
183
+ const bool expectedOutput = true ; // /< Expected output of this test
187
184
188
185
log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
189
186
" ~" );
190
187
log (" This is test case 1 for sublist search Algorithm : " );
191
188
log (" Description:" );
192
189
log (" EDGE CASE : Only contains one element" );
193
190
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
199
193
200
194
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
203
196
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
206
198
207
199
bool exists = search::sublist_search::sublistSearch (
208
- sublistLL, mainlistLL); // / boolean, if sublist exist or not
200
+ sublistLL, mainlistLL); // /< boolean, if sublist exist or not
209
201
210
202
log (" Checking assert expression..." );
211
203
assert (exists == expectedOutput);
@@ -227,16 +219,13 @@ class TestCases {
227
219
void testCase_2 () {
228
220
const bool expectedOutput = true ; // / Expected output of this test
229
221
230
- log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
231
- " ~" );
222
+ log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
232
223
log (" This is test case 2 for sublist search Algorithm : " );
233
224
log (" Description:" );
234
225
log (" contains main list of 100 elements and sublist of 20" );
235
226
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
240
229
241
230
for (int i = 0 ; i < 100 ; i++) {
242
231
// / Inserts 100 elements in main list
@@ -251,22 +240,19 @@ class TestCases {
251
240
}
252
241
253
242
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
256
244
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
259
246
260
247
bool exists = search::sublist_search::sublistSearch (
261
- sublistLL, mainlistLL); // boolean, if sublist exist or not
248
+ sublistLL, mainlistLL); // /< boolean, if sublist exist or not
262
249
263
250
log (" Checking assert expression..." );
264
251
assert (exists == expectedOutput);
265
252
log (" Assertion check passed!" );
266
253
267
254
log (" [PASS] : TEST CASE 2 PASS!" );
268
- log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
269
- " ~" );
255
+ log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
270
256
}
271
257
272
258
/* *
@@ -275,17 +261,15 @@ class TestCases {
275
261
* @returns void
276
262
* */
277
263
void testCase_3 () {
278
- const bool expectedOutput = false ; // / Expected output of this test
264
+ const bool expectedOutput = false ; // /< Expected output of this test
279
265
280
- log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
281
- " ~" );
266
+ log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
282
267
log (" This is test case 3 for sublist search Algorithm : " );
283
268
log (" Description:" );
284
269
log (" contains main list of 50 elements and sublist of 20" );
285
270
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
289
273
290
274
for (int i = 0 ; i < 50 ; i++) {
291
275
// / Inserts 100 elements in main list
@@ -298,22 +282,19 @@ class TestCases {
298
282
}
299
283
300
284
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
303
286
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
306
288
307
289
bool exists = search::sublist_search::sublistSearch (
308
- sublistLL, mainlistLL); // / boolean, if sublist exist or not
290
+ sublistLL, mainlistLL); // /< boolean, if sublist exist or not
309
291
310
292
log (" Checking assert expression..." );
311
293
assert (exists == expectedOutput);
312
294
log (" Assertion check passed!" );
313
295
314
296
log (" [PASS] : TEST CASE 3 PASS!" );
315
- log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
316
- " ~" );
297
+ log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
317
298
}
318
299
};
319
300
@@ -339,12 +320,12 @@ int main(int argc, char *argv[]) {
339
320
std::vector<uint64_t > sublistData = {6 , 8 };
340
321
341
322
search::sublist_search::Node *mainlistLL =
342
- search::sublist_search::makeLinkedList (mainlistData);
323
+ search::sublist_search::makeLinkedList (mainlistData);
343
324
search::sublist_search::Node *sublistLL =
344
- search::sublist_search::makeLinkedList (sublistData);
325
+ search::sublist_search::makeLinkedList (sublistData);
345
326
346
327
bool exists = search::sublist_search::sublistSearch (
347
- sublistLL, mainlistLL); // boolean, if sublist exist or not
328
+ sublistLL, mainlistLL); // boolean, if sublist exist or not
348
329
349
330
std::cout << " Sublist :" << std::endl;
350
331
search::sublist_search::printLinkedList (sublistLL);
0 commit comments