TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
sublist_search.cpp
Go to the documentation of this file.
1
27
28
#include <cassert>
29
#include <cstdint>
30
#include <iostream>
31
#include <vector>
32
37
namespace
search
{
44
namespace
sublist_search
{
48
struct
Node
{
49
uint32_t
data
= 0;
50
Node
*
next
{};
51
};
52
58
void
printLinkedList
(
Node
*start) {
59
while
(start !=
nullptr
) {
60
std::cout <<
"->"
<< start->data;
61
start = start->next;
62
}
63
std::cout << std::endl;
64
}
65
74
Node
*
makeLinkedList
(
const
std::vector<uint64_t> &
data
) {
77
Node
*head =
nullptr
;
78
Node
*tail =
nullptr
;
79
for
(
int
i :
data
) {
80
Node
*
node
=
new
Node
;
81
node
->data = i;
82
node
->next =
nullptr
;
83
if
(head ==
nullptr
) {
84
head =
node
;
85
tail =
node
;
86
}
else
{
87
tail->
next
=
node
;
88
tail = tail->
next
;
89
}
90
}
91
return
head;
92
}
93
94
/*
95
* @brief This function dealocates memory related to the given list
96
* It recursively deletes all of the nodes of the input list.
97
* @param room the root/head of the input list
98
* @warning Plese note that the memory for each node has to be alocated using
99
* new.
100
*/
101
void
deleteList(
Node
*
const
root) {
102
if
(root != NULL) {
103
deleteList(root->next);
104
delete
root;
105
}
106
}
107
115
bool
sublistSearch
(
Node
*sublist,
Node
*mainList) {
116
if
(sublist ==
nullptr
|| mainList ==
nullptr
) {
117
return
false
;
118
}
119
121
Node
*target_ptr = sublist;
122
123
while
(mainList !=
nullptr
) {
125
Node
*main_ptr = mainList;
126
127
while
(target_ptr !=
nullptr
) {
128
if
(main_ptr ==
nullptr
) {
129
return
false
;
130
131
}
else
if
(main_ptr->
data
== target_ptr->
data
) {
134
target_ptr = target_ptr->
next
;
135
main_ptr = main_ptr->
next
;
136
137
}
else
{
138
break
;
139
}
140
}
141
142
if
(target_ptr ==
nullptr
) {
146
return
true
;
147
}
148
150
target_ptr = sublist;
151
154
mainList = mainList->
next
;
155
}
156
159
return
false
;
160
}
161
162
}
// namespace sublist_search
163
}
// namespace search
164
168
class
TestCases
{
169
private
:
175
template
<
typename
T>
176
void
log
(T msg) {
177
// It's just to avoid writing cout and endl
178
std::cout <<
"[TESTS] : ---> "
<< msg << std::endl;
179
}
180
181
public
:
186
void
runTests
() {
187
log
(
"Running Tests..."
);
188
189
testCase_1
();
190
testCase_2
();
191
testCase_3
();
192
193
log
(
"Test Cases over!"
);
194
std::cout << std::endl;
195
}
196
201
void
testCase_1
() {
202
const
bool
expectedOutput =
true
;
203
204
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
205
"~"
);
206
log
(
"This is test case 1 for sublist search Algorithm : "
);
207
log
(
"Description:"
);
208
log
(
" EDGE CASE : Only contains one element"
);
209
210
std::vector<uint64_t> sublistData = {
211
6};
212
std::vector<uint64_t> mainlistData = {
213
2, 5, 6, 7,
214
8};
215
216
search::sublist_search::Node
*sublistLL =
217
search::sublist_search::makeLinkedList
(
218
sublistData);
219
search::sublist_search::Node
*mainlistLL =
220
search::sublist_search::makeLinkedList
(
221
mainlistData);
223
224
bool
exists =
search::sublist_search::sublistSearch
(
225
sublistLL, mainlistLL);
226
227
log
(
"Checking assert expression..."
);
228
assert(exists == expectedOutput);
229
log
(
"Assertion check passed!"
);
230
231
log
(
"[PASS] : TEST CASE 1 PASS!"
);
232
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
233
"~"
);
234
235
deleteList(mainlistLL);
236
deleteList(sublistLL);
237
}
238
244
void
testCase_2
() {
245
const
bool
expectedOutput =
true
;
246
247
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
248
"~"
);
249
log
(
"This is test case 2 for sublist search Algorithm : "
);
250
log
(
"Description:"
);
251
log
(
" contains main list of 100 elements and sublist of 20"
);
252
253
std::vector<uint64_t> sublistData(
254
20);
255
std::vector<uint64_t> mainlistData(
256
100);
257
258
for
(
int
i = 0; i < 100; i++) {
260
mainlistData[i] = i + 1;
261
}
262
263
int
temp = 0;
264
for
(
int
i = 45; i < 65; i++) {
266
sublistData[temp] = i + 1;
267
temp++;
268
}
269
270
search::sublist_search::Node
*sublistLL =
271
search::sublist_search::makeLinkedList
(
272
sublistData);
273
search::sublist_search::Node
*mainlistLL =
274
search::sublist_search::makeLinkedList
(
275
mainlistData);
277
278
bool
exists =
search::sublist_search::sublistSearch
(
279
sublistLL, mainlistLL);
280
281
log
(
"Checking assert expression..."
);
282
assert(exists == expectedOutput);
283
log
(
"Assertion check passed!"
);
284
285
log
(
"[PASS] : TEST CASE 2 PASS!"
);
286
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
287
"~"
);
288
289
deleteList(mainlistLL);
290
deleteList(sublistLL);
291
}
292
298
void
testCase_3
() {
299
const
bool
expectedOutput =
false
;
300
301
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
302
"~"
);
303
log
(
"This is test case 3 for sublist search Algorithm : "
);
304
log
(
"Description:"
);
305
log
(
" contains main list of 50 elements and sublist of 20"
);
306
307
std::vector<uint64_t> sublistData(20);
308
std::vector<uint64_t> mainlistData(
309
50);
310
311
for
(
int
i = 0; i < 50; i++) {
313
mainlistData.push_back(i + 1);
314
}
315
316
for
(
int
i = 45; i < 65; i++) {
318
sublistData.push_back(i + 1);
319
}
320
321
search::sublist_search::Node
*sublistLL =
322
search::sublist_search::makeLinkedList
(
323
sublistData);
324
search::sublist_search::Node
*mainlistLL =
325
search::sublist_search::makeLinkedList
(
326
mainlistData);
328
329
bool
exists =
search::sublist_search::sublistSearch
(
330
sublistLL, mainlistLL);
331
332
log
(
"Checking assert expression..."
);
333
assert(exists == expectedOutput);
334
log
(
"Assertion check passed!"
);
335
336
log
(
"[PASS] : TEST CASE 3 PASS!"
);
337
log
(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
338
"~"
);
339
340
deleteList(mainlistLL);
341
deleteList(sublistLL);
342
}
343
};
344
349
static
void
test
() {
350
TestCases
tc;
351
tc.
runTests
();
352
}
353
358
int
main
() {
359
test
();
// run self-test implementations
360
361
std::vector<uint64_t> mainlistData = {
362
2, 5, 6, 7, 8};
363
std::vector<uint64_t> sublistData = {6, 8};
364
365
search::sublist_search::Node
*mainlistLL =
366
search::sublist_search::makeLinkedList
(mainlistData);
367
search::sublist_search::Node
*sublistLL =
368
search::sublist_search::makeLinkedList
(
369
sublistData);
371
372
bool
exists =
search::sublist_search::sublistSearch
(
373
sublistLL,
374
mainlistLL);
375
376
std::cout <<
"Sublist: "
<< std::endl;
377
search::sublist_search::printLinkedList
(sublistLL);
378
379
std::cout <<
"Main list: "
<< std::endl;
380
search::sublist_search::printLinkedList
(mainlistLL);
381
std::cout << std::endl;
382
383
if
(exists) {
384
std::cout <<
"[TRUE] - sublist found in main list\n"
;
385
}
else
{
386
std::cout <<
"[FALSE] - sublist NOT found in main list\n"
;
387
}
388
389
deleteList(mainlistLL);
390
deleteList(sublistLL);
391
return
0;
392
}
test
void test()
Definition
caesar_cipher.cpp:100
TestCases
class encapsulating the necessary test cases
Definition
inorder_successor_of_bst.cpp:225
TestCases::log
void log(T msg)
A function to print given message on console.
Definition
sublist_search.cpp:176
TestCases::testCase_2
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
Definition
inorder_successor_of_bst.cpp:304
TestCases::testCase_1
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
Definition
inorder_successor_of_bst.cpp:259
TestCases::testCase_3
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.
Definition
inorder_successor_of_bst.cpp:345
TestCases::runTests
void runTests()
Executes test cases.
Definition
sublist_search.cpp:186
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data
int data[MAX]
test data
Definition
hash_search.cpp:24
search
for std::assert
Definition
binary_search.cpp:47
sublist_search
Functions for the Sublist Search implementation.
Node
Definition
linkedlist_implentation_usingarray.cpp:14
node
Definition
binary_search_tree.cpp:11
search::sublist_search::Node
A Node structure representing a single link Node in a linked list.
Definition
sublist_search.cpp:48
search::sublist_search::Node::data
uint32_t data
the key/value of the node
Definition
sublist_search.cpp:49
search::sublist_search::Node::next
Node * next
pointer to the next node
Definition
sublist_search.cpp:50
search::sublist_search::sublistSearch
bool sublistSearch(Node *sublist, Node *mainList)
Main searching function.
Definition
sublist_search.cpp:115
search::sublist_search::makeLinkedList
Node * makeLinkedList(const std::vector< uint64_t > &data)
Give a vector of data, it adds each element of vector in the linked list and return the address of he...
Definition
sublist_search.cpp:74
search::sublist_search::printLinkedList
void printLinkedList(Node *start)
A simple function to print the linked list.
Definition
sublist_search.cpp:58
search
sublist_search.cpp
Generated by
1.18.0