TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
sublist_search.cpp
Go to the documentation of this file.
1
28#include <cassert>
29#include <cstdint>
30#include <iostream>
31#include <vector>
32
37namespace search {
44namespace sublist_search {
48struct Node {
49 uint32_t data = 0;
51};
52
58void printLinkedList(Node *start) {
59 while (start != nullptr) {
60 std::cout << "->" << start->data;
61 start = start->next;
62 }
63 std::cout << std::endl;
64}
65
74Node *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 */
101void deleteList(Node *const root) {
102 if (root != NULL) {
103 deleteList(root->next);
104 delete root;
105 }
106}
107
115bool 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
168class 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
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
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
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
349static void test() {
350 TestCases tc;
351 tc.runTests();
352}
353
360int main(int argc, char *argv[]) {
361 test(); // run self-test implementations
362
363 std::vector<uint64_t> mainlistData = {
364 2, 5, 6, 7, 8};
365 std::vector<uint64_t> sublistData = {6, 8};
366
367 search::sublist_search::Node *mainlistLL =
368 search::sublist_search::makeLinkedList(mainlistData);
370 search::sublist_search::makeLinkedList(
371 sublistData);
373
374 bool exists = search::sublist_search::sublistSearch(
375 sublistLL,
376 mainlistLL);
377
378 std::cout << "Sublist: " << std::endl;
379 search::sublist_search::printLinkedList(sublistLL);
380
381 std::cout << "Main list: " << std::endl;
382 search::sublist_search::printLinkedList(mainlistLL);
383 std::cout << std::endl;
384
385 if (exists) {
386 std::cout << "[TRUE] - sublist found in main list\n";
387 } else {
388 std::cout << "[FALSE] - sublist NOT found in main list\n";
389 }
390
391 deleteList(mainlistLL);
392 deleteList(sublistLL);
393 return 0;
394}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
class encapsulating the necessary test cases
void log(T msg)
A function to print given message on console.
void testCase_2()
A test case which contains main list of 100 elements and sublist of 20.
void testCase_1()
A test case contains edge case, printing inorder successor of last node.
void testCase_3()
A test case which contains main list of 50 elements and sublist of 20.
void runTests()
Executes test cases.
int main()
Main function.
int data[MAX]
test data
for std::assert
Functions for the Sublist Search implementation.
A Node structure representing a single link Node in a linked list.
uint32_t data
the key/value of the node
Node * next
pointer to the next node
bool sublistSearch(Node *sublist, Node *mainList)
Main searching function.
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...
static void test()
Self-test implementations.
void printLinkedList(Node *start)
A simple function to print the linked list.