-
Notifications
You must be signed in to change notification settings - Fork 15
/
A7.cpp
342 lines (283 loc) · 4.71 KB
/
A7.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <iostream>
using namespace std;
class node{
string key,type,ival;
int chain;
public:
friend class HashTable;
};
class HashTable{
node* HT;
int n;
public:
HashTable(int d){
n=d;
HT=new node[n];
for(int i=0;i<n;i++){
HT[i].key="#";
HT[i].chain=-1;
}
}
void create();
void withoutinsert(node); //linear probing without replacement
void withinsert(node); //linear probing with replacement
void find();
void del();
void modify();
};
void HashTable::modify(){
string k,newival;
int v,u;
cout<<"\nEnter key of element to be modified";
cin>>k;
cout<<"\nEnter new initial value: ";
cin>>newival;
int l=k.length()%n;
if(HT[l].key==k)
HT[l].ival=newival;
else{
int v;
v=l;
u=HT[l].chain;
while(u!=-1&&HT[v].key!=k)
{
v=u;
u=HT[v].chain;
}
if(HT[v].key==k)
HT[v].ival=newival;
else
cout<<"\nEntry does not exists!";
}
}
void HashTable::del(){
string k;
int v,u;
cout<<"\nEnter key of element to be deleted";
cin>>k;
int l=k.length()%n;
if(HT[l].key==k){
HT[l].key="#";
HT[l].type="";
HT[l].ival="";
cout<<"\nDELETED SUCCESSFULLY";
}
else{
int v;
v=l;
u=HT[l].chain;
while(u!=-1&&HT[v].key!=k)
{
v=u;
u=HT[v].chain;
}
if(HT[v].key==k){
HT[v].key="#";
HT[v].type="";
HT[v].ival="";
cout<<"\nDELETED SUCCESSFULLY";
}
else
cout<<"\nENTRY DOES NOT EXISTS";
}
}
void HashTable::find(){
string k;
int v,u;
cout<<"\nEnter key of element to be found";
cin>>k;
int l=k.length()%n;
if(HT[l].key==k)
cout<<"\nFOUND!\n"<<HT[l].key<<HT[l].type<<HT[l].ival;
else{
int v;
v=l;
u=HT[l].chain;
while(u!=-1&&HT[v].key!=k)
{
v=u;
u=HT[v].chain;
}
if(HT[v].key==k)
cout<<"\nFOUND!\n"<<HT[v].key<<HT[v].type<<HT[v].ival;
else
cout<<"\nNOT FOUND!";
}
}
void HashTable::withinsert(node nn)
{
int l=nn.key.length()%n;
if(HT[l].key=="#")
{
HT[l]=nn;
HT[l].chain=-1;
}
else
{
if(HT[l].key.length()%n==l)
{int v,u;
v=l;
u=HT[l].chain;
while(u!=-1)
{
v=u;
u=HT[v].chain;
}
int i;
for(i=(l+1)%n;HT[i].key!="#";i=(i+1)%n);
HT[i]=nn;
HT[v].chain=i;
HT[i].chain=-1;
}
else
{
node temp;
temp=HT[l];
HT[l]=nn;
HT[l].chain=-1;
int i;
for(i=(l+1)%n;HT[i].key!="#";i=(i+1)%n);
HT[i]=temp;
int k;
for(k=0;HT[k].chain!=l;k=(k+1)%n);
HT[k].chain=i;
HT[i].chain=-1;
}
}
for(int i=0;i<n;i++)
cout<<"\n"<<HT[i].key<<"\t"<<HT[i].chain;
}
void HashTable::withoutinsert(node nn)
{
int v,u;
int l=nn.key.length()%n;
if(HT[l].key=="#"){
HT[l]=nn;
HT[l].chain=-1;
}
else{
v=l;
u=HT[l].chain;
while(u!=-1)
{
v=u;
u=HT[v].chain;
}
int i;
for(i=(l+1)%n;HT[i].key!="#";i=(i+1)%n);
HT[i]=nn;
HT[v].chain=i;
HT[i].chain=-1;
}
for(int i=0;i<n;i++)
cout<<"\n"<<HT[i].key<<"\t"<<HT[i].chain;
}
void HashTable::create(){
int ch;
cout<<"\nEnter type of Linear Probing: \n1. Without Replacement\n2. With Replacement";
cin>>ch;
for(int i=0;i<n;i++)
{ node nn;
cout<<"\n"<<i+1<<" Entry";
cout<<"\nEnter Identifier(symbol) Name: ";
cin>>nn.key;
cout<<"Enter data type: ";
cin>>nn.type;
cout<<"Enter initial value: ";
cin>>nn.ival;
if(ch==1)
withoutinsert(nn);
if(ch==2)
withinsert(nn);
}
}
int main(){
int n;
cout<<"\nEnter number of Identifiers(symbols) to be stored: ";
cin>>n;
HashTable obj(n);
obj.create();
do{
cout<<"\nSELECT OPTION\n1. Retrieve\n2. Delete\n3. Modify\n4. Exit";
cin>>n;
if(n==1)
obj.find();
if(n==2)
obj.del();
if(n==3)
obj.modify();
}
while(n!=4);
return 0;
}
/*
shreyas@shreyas:~/2416$ g++ A7.cpp
shreyas@shreyas:~/2416$ ./a.out
Enter number of Identifiers(symbols) to be stored: 3
Enter type of Linear Probing:
1. Without Replacement
2. With Replacement2
1 Entry
Enter Identifier(symbol) Name: dot
Enter data type: char
Enter initial value: 0
dot -1
# -1
# -1
2 Entry
Enter Identifier(symbol) Name: dash
Enter data type: char
Enter initial value: 1
dot -1
dash -1
# -1
3 Entry
Enter Identifier(symbol) Name: dollar
Enter data type: int
Enter initial value: 2
dot 2
dash -1
dollar -1
SELECT OPTION
1. Retrieve
2. Delete
3. Modify
4. Exit1
Enter key of element to be found2
NOT FOUND!
SELECT OPTION
1. Retrieve
2. Delete
3. Modify
4. Exit1
Enter key of element to be found-1
NOT FOUND!
SELECT OPTION
1. Retrieve
2. Delete
3. Modify
4. Exit1
Enter key of element to be founddot
FOUND!
dotchar0
SELECT OPTION
1. Retrieve
2. Delete
3. Modify
4. Exit3
Enter key of element to be modifieddot
Enter new initial value: 5
SELECT OPTION
1. Retrieve
2. Delete
3. Modify
4. Exit2
Enter key of element to be deleteddash
DELETED SUCCESSFULLY
SELECT OPTION
1. Retrieve
2. Delete
3. Modify
4. Exit4
shreyas@shreyas:~/2416$
*/