-
Notifications
You must be signed in to change notification settings - Fork 45
/
A1.cpp
211 lines (181 loc) · 4.1 KB
/
A1.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <iomanip>
using namespace std;
#define size 13
class linear_probing {
int hash[size], flag[size];
public:
linear_probing() {
int i;
for (i = 0; i < size; i++) {
flag[i] = 0;
}
}
void insert(int x) {
int i, loc;
loc = x % size;
for (i = 0; i < size; i++) {
if (flag[loc] == 0) {
hash[loc] = x;
flag[loc] = 1;
break;
}
else {
loc = (loc + 1) % size;
}
}
}
void create() {
int x, i, n;
cout << "\nEnter the number of phone numbers to be inserted:\n";
cin >> n;
cout << "\nEnter the Phone numbers to be inserted in Hash table ->\n";
for (i = 0; i < n; i++) {
cin >> x;
insert(x);
}
}
int find(int x) {
int loc = x % size;
int i;
for (i = 0; i < size; i++) {
if (flag[loc] == 1 && hash[loc] == x) {
return loc;
}
else
loc = (loc + 1) % size;
}
return -1;
}
void search() {
int x, loc;
cout << "\nEnter the phone number to be search:\n";
cin >> x;
if ((loc = find(x)) == -1)
cout << "\nPhone number is not found\n";
else
cout << "\nPhone number " << x << " is found at " << loc << "th location\n\n";
}
void print() {
int i;
cout << "\nHash Table is ->"
<< endl;
for (i = 0; i < size; i++) {
cout << "(" << i << ") -> ";
if (flag[i] == 1) {
cout << hash[i] << "\n";
}
else if (flag[i] == 0)
cout << "----\n";
}
}
};
struct node {
node *next;
int phone;
};
class Chaining {
node *hash[size];
public:
Chaining() {
int i;
for (i = 0; i < size; i++)
hash[i] = NULL;
}
void create() {
int x, n, i;
cout << "\nEnter the number of phone numbers to be inserted :\n";
cin >> n;
cout << "\nEnter the Phone numbers to be inserted in Hash table ->\n";
for (i = 0; i < n; i++) {
cin >> x;
insert(x);
}
}
void insert(int key) {
int loc = key % size;
node *p = new node;
p->phone = key;
p->next = NULL;
if (hash[loc] == NULL) {
hash[loc] = p;
}
else {
node *q = hash[loc];
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
void display() {
int i;
cout << "\nHash Table is -> :\n";
for (i = 0; i < size; i++) {
node *q = hash[i];
cout << "(" << i << ") -> ";
while (q) {
cout << q->phone;
if (q->next)
cout << " -> ";
q = q->next;
}
cout << "\n";
}
}
};
int main() {
cout << "----------------- Linear Probing -----------------\n";
linear_probing lp;
lp.create();
lp.print();
lp.search();
cout << "\n----------------- Separate Chaining -----------------\n";
Chaining h;
h.create();
h.display();
return 0;
}
/*
----------------- Linear Probing -----------------
Enter the number of phone numbers to be inserted:
5
Enter the phone numbers to be inserted in hash table ->
123 136 284 297 448
Hash Table is ->
(0) -> ----
(1) -> ----
(2) -> ----
(3) -> ----
(4) -> ----
(5) -> ----
(6) -> 123
(7) -> 136
(8) -> 448
(9) -> ----
(10) -> ----
(11) -> 284
(12) -> 297
Enter the phone number to be search:
448
Phone number 448 is found at 8th location
----------------- Separate Chaining -----------------
Enter the number of phone numbers to be inserted :
5
Enter the Phone numbers to be inserted in Hash table
123 136 284 297 448
Hash Table is -> :
(0) ->
(1) ->
(2) ->
(3) ->
(4) ->
(5) ->
(6) -> 123 -> 136 -> 448
(7) ->
(8) ->
(9) ->
(10) ->
(11) -> 284 -> 297
(12) ->
*/