forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SublistSearch.cpp
83 lines (68 loc) · 1.33 KB
/
SublistSearch.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
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* next;
};
bool findList(Node* first, Node* second)
{
Node* ptr1 = first, *ptr2 = second;
if (first == NULL && second == NULL)
return true;
if ( first == NULL ||
(first != NULL && second == NULL))
return false;
while (second != NULL)
{
ptr2 = second;
while (ptr1 != NULL)
{
if (ptr2 == NULL)
return false;
else if (ptr1->data == ptr2->data)
{
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
else break;
}
if (ptr1 == NULL)
return true;
ptr1 = first;
second = second->next;
}
return false;
}
void printList(Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
Node *newNode(int key)
{
Node *temp = new Node;
temp-> data= key;
temp->next = NULL;
return temp;
}
/* Driver program to test above functions*/
int main()
{
Node *a = newNode(1);
a->next = newNode(2);
a->next->next = newNode(3);
a->next->next->next = newNode(4);
Node *b = newNode(1);
b->next = newNode(2);
b->next->next = newNode(1);
b->next->next->next = newNode(2);
b->next->next->next->next = newNode(3);
b->next->next->next->next->next = newNode(4);
findList(a,b) ? cout << "LIST FOUND" :
cout << "LIST NOT FOUND";
return 0;
}