59 while (start !=
nullptr) {
60 std::cout <<
"->" << start->data;
63 std::cout << std::endl;
83 if (head ==
nullptr) {
101void deleteList(
Node *
const root) {
103 deleteList(root->next);
116 if (sublist ==
nullptr || mainList ==
nullptr) {
121 Node *target_ptr = sublist;
123 while (mainList !=
nullptr) {
125 Node *main_ptr = mainList;
127 while (target_ptr !=
nullptr) {
128 if (main_ptr ==
nullptr) {
131 }
else if (main_ptr->
data == target_ptr->
data) {
134 target_ptr = target_ptr->
next;
135 main_ptr = main_ptr->
next;
142 if (target_ptr ==
nullptr) {
150 target_ptr = sublist;
154 mainList = mainList->
next;
175 template <
typename T>
178 std::cout <<
"[TESTS] : ---> " << msg << std::endl;
187 log(
"Running Tests...");
193 log(
"Test Cases over!");
194 std::cout << std::endl;
202 const bool expectedOutput =
true;
204 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
206 log(
"This is test case 1 for sublist search Algorithm : ");
208 log(
" EDGE CASE : Only contains one element");
210 std::vector<uint64_t> sublistData = {
212 std::vector<uint64_t> mainlistData = {
217 search::sublist_search::makeLinkedList(
220 search::sublist_search::makeLinkedList(
224 bool exists = search::sublist_search::sublistSearch(
225 sublistLL, mainlistLL);
227 log(
"Checking assert expression...");
228 assert(exists == expectedOutput);
229 log(
"Assertion check passed!");
231 log(
"[PASS] : TEST CASE 1 PASS!");
232 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
235 deleteList(mainlistLL);
236 deleteList(sublistLL);
245 const bool expectedOutput =
true;
247 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
249 log(
"This is test case 2 for sublist search Algorithm : ");
251 log(
" contains main list of 100 elements and sublist of 20");
253 std::vector<uint64_t> sublistData(
255 std::vector<uint64_t> mainlistData(
258 for (
int i = 0; i < 100; i++) {
260 mainlistData[i] = i + 1;
264 for (
int i = 45; i < 65; i++) {
266 sublistData[temp] = i + 1;
271 search::sublist_search::makeLinkedList(
274 search::sublist_search::makeLinkedList(
278 bool exists = search::sublist_search::sublistSearch(
279 sublistLL, mainlistLL);
281 log(
"Checking assert expression...");
282 assert(exists == expectedOutput);
283 log(
"Assertion check passed!");
285 log(
"[PASS] : TEST CASE 2 PASS!");
286 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
289 deleteList(mainlistLL);
290 deleteList(sublistLL);
299 const bool expectedOutput =
false;
301 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
303 log(
"This is test case 3 for sublist search Algorithm : ");
305 log(
" contains main list of 50 elements and sublist of 20");
307 std::vector<uint64_t> sublistData(20);
308 std::vector<uint64_t> mainlistData(
311 for (
int i = 0; i < 50; i++) {
313 mainlistData.push_back(i + 1);
316 for (
int i = 45; i < 65; i++) {
318 sublistData.push_back(i + 1);
322 search::sublist_search::makeLinkedList(
325 search::sublist_search::makeLinkedList(
329 bool exists = search::sublist_search::sublistSearch(
330 sublistLL, mainlistLL);
332 log(
"Checking assert expression...");
333 assert(exists == expectedOutput);
334 log(
"Assertion check passed!");
336 log(
"[PASS] : TEST CASE 3 PASS!");
337 log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
340 deleteList(mainlistLL);
341 deleteList(sublistLL);
360int main(
int argc,
char *argv[]) {
363 std::vector<uint64_t> mainlistData = {
365 std::vector<uint64_t> sublistData = {6, 8};
368 search::sublist_search::makeLinkedList(mainlistData);
370 search::sublist_search::makeLinkedList(
374 bool exists = search::sublist_search::sublistSearch(
378 std::cout <<
"Sublist: " << std::endl;
379 search::sublist_search::printLinkedList(sublistLL);
381 std::cout <<
"Main list: " << std::endl;
382 search::sublist_search::printLinkedList(mainlistLL);
383 std::cout << std::endl;
386 std::cout <<
"[TRUE] - sublist found in main list\n";
388 std::cout <<
"[FALSE] - sublist NOT found in main list\n";
391 deleteList(mainlistLL);
392 deleteList(sublistLL);
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
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.
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.