Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of the Sublist Search Algorithm More...
#include <cassert>
#include <iostream>
#include <vector>
Classes | |
struct | search::sublist_search::Node |
A Node structure representing a single link Node in a linked list. More... | |
class | TestCases |
class encapsulating the necessary test cases More... | |
Namespaces | |
namespace | search |
for std::vector | |
namespace | sublist_search |
Functions for the Sublist Search implementation. | |
Functions | |
void | search::sublist_search::printLinkedList (Node *start) |
A simple function to print the linked list. | |
Node * | search::sublist_search::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 head pointer. | |
void | search::sublist_search::deleteList (Node *const root) |
bool | search::sublist_search::sublistSearch (Node *sublist, Node *mainList) |
Main searching function. | |
static void | test () |
Self-test implementations. | |
int | main (int argc, char *argv[]) |
Main function. | |
Implementation of the Sublist Search Algorithm
void search::sublist_search::deleteList | ( | Node *const | root | ) |
int main | ( | int | argc, |
char * | argv[] ) |
Main function.
argc | commandline argument count (ignored) |
argv | commandline array of arguments (ignored) |
< Main list in which sublist is to be searched
< Sublist to be searched
< Main list in which sublist is to be searched
< boolean to check if the sublist exists or not
Node * search::sublist_search::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 head pointer.
data | A vector of "int" containing the data that is supposed to be stored in nodes of linked list. |
This is used in test cases for rapidly creating linked list with 100+ elements, instead of hard-coding 100 elements in test cases.
void search::sublist_search::printLinkedList | ( | Node * | start | ) |
Main searching function.
sublist | A linked list which is supposed to be searched in mainList. |
mainList | A linked list in which sublist will be searched. |
Initialize target pointer to the head node of sublist.
Initialize main pointer to the current node of main list.
If the data of target node and main node is equal then move to the next node of both lists.
Is target pointer becomes null that means the target list is been traversed without returning false. Which means the sublist has been found and return ture.
set the target pointer again to stating point of target list.
set the main pointer to the next element of the main list and repeat the algo.
If the main list is exhausted, means sublist does not found, return false