-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbsearchLinklist.cpp
More file actions
122 lines (100 loc) · 2.18 KB
/
bsearchLinklist.cpp
File metadata and controls
122 lines (100 loc) · 2.18 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define MOD 1000000007
struct node{
int info;
node *link;
};
void printList(node* n) {
while (n != NULL) {
cout << n->info << " ";
n = n->link;
}
}
void push(node** head_ref, int new_data) {
node* new_node = new node();
new_node->link = NULL ;
new_node->info = new_data ;
node* q = new node();
if(*head_ref == NULL){
*head_ref = new_node;
}
else{
q = *head_ref;
while(q->link!=NULL)
{
q=q->link;
}
q->link=new_node;
}
}
node* middle(node* start, node* last)
{
if (start == NULL)
return NULL;
node* slow = start;
node* fast = start -> link ;
while (fast != last)
{
fast = fast -> link;
if (fast != last)
{
slow = slow -> link;
fast = fast -> link;
}
}
return slow ;
}
node* binarySearch(node *head, int value)
{
struct node* start = head;
struct node* last = NULL;
do
{
// Find middle
node* mid = middle(start, last);
// If middle is empty
if (mid == NULL)
return NULL;
// If value is present at middle
if (mid -> info == value)
return mid;
// If value is more than mid
else if (mid -> info < value)
start = mid -> link;
// If the value is less than mid.
else
last = mid;
} while (last == NULL ||
last != start);
// value not present
return NULL;
}
void CowboyBebop(node *a){
int b ;
cin >> b ;
if (binarySearch(a, b) == NULL)
printf("Value not present\n");
else
printf("Present");
}
using namespace std;
int main(void){
std::ios_base::sync_with_stdio(false);
// #ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
node* a = NULL ;
int t = 1 , n , d ;
cin >> n ;
for (int i = 0; i < n; i++)
{
cin >> d ;
push(&a,d);
}
while(t--){
CowboyBebop(a);
}
return 0;
}