forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublist_Search.cpp
111 lines (95 loc) · 2.2 KB
/
sublist_Search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
SUBLIST SEARCH
to search if a given sublist is present in a mainList using LinkedList.
*/
#include <bits/stdc++.h>
using namespace std;
//A linkedlist Node
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* getNode(int data){
Node* n = new Node;
n->data = data;
n->next = NULL;
return n;
}
//returns true if the given sublist is found in MainList.
bool searchSublist(Node* mainList,Node* subList){
Node *main = mainList, *sub = subList;
//if both the lists are empty, return true.
if(main == NULL && sub == NULL)
return true;
//if one list is empty and other is not return false
else if(main == NULL ||(main != NULL && sub == NULL))
return false;
while(mainList != NULL){
main = mainList;
//searching for the sublist in mainlist.
while(sub != NULL){
if(main == NULL)
return false;
else if(main->data == sub->data){
main = main->next;
sub = sub->next;
}else
break;
}
//if the sub list is completely found in Main list, succesful search.
if(sub == NULL)
return true;
sub = subList;
mainList = mainList->next;
}
return false;
}
Node* insertToList(Node* head,int data){
if(head == NULL){
head = getNode(data);
}else{
Node *ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = getNode(data);
}
return head;
}
int main(){
Node *mainHead = NULL,*subHead = NULL;
int data,sizeMainlist,sizeSublist;
//Input MainList in which search is to be done
cout<<"Enter number of elements in Main List: ";
cin>>sizeMainlist;
cout<<"Enter Main List: ";
for(int i=0;i<sizeMainlist;i++){
cin>>data;
mainHead = insertToList(mainHead,data);
}
//Input sublist which is to searched for
cout<<"Enter number of elements in Sub List: ";
cin>>sizeSublist;
cout<<"Enter Sub List: ";
for(int i=0;i<sizeSublist;i++){
cin>>data;
subHead = insertToList(subHead,data);
}
//Printing the search result
if(searchSublist(mainHead,subHead))
cout<<"SEARCH SUCCESSFUL";
else
cout<<"SEARCH UNSUCCESSFUL";
return 0;
}
/*
Time Complexity: O(sizeMainlist * sizeSublist)
CASE 1:
MAIN: 1 2 3 4 5 6 7 8
SUB: 2 3 4
Expected Output: SEARCH SUCCESSFUL
CASE 1:
MAIN: 1 44 3 13 12 6 19 8
SUB: 13 12 12
Expected Output: SEARCH UNSUCCESSFUL
*/