-
Notifications
You must be signed in to change notification settings - Fork 15
/
A6.cpp
258 lines (204 loc) · 3.27 KB
/
A6.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
#include <iostream>
using namespace std;
class node{
string key,meaning;
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="#";
}
void create();
void withoutinsert(node); //linear probing without replacement
void withinsert(node); //linear probing with replacement
void find();
void del();
};
void HashTable::del()
{
string k;
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].meaning="";
cout<<"\nDELETED SUCCESSFULLY";
}
else
{
int i;
for(i=(l+1)%n;i!=l&&HT[i].key!=k;i=(i+1)%n);
if(i==l)
cout<<"\nENTRY DOES NOT EXISTS";
else
{
HT[i].key="#";
HT[i].meaning="";
cout<<"\nDELETED SUCCESSFULLY";
}
}
}
void HashTable::find()
{
string k;
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].meaning;
else
{ int i;
for(i=(l+1)%n;i!=l&&HT[i].key!=k;i=(i+1)%n);
if(i==l)
cout<<"\nNOT FOUND!";
else
cout<<"\nFOUND!\n"<<HT[i].key<<" - "<<HT[i].meaning;
}
}
void HashTable::withoutinsert(node nn)
{
int l=nn.key.length()%n;
int i;
for(i=l;HT[i].key!="#";i=(i+1)%n);
HT[i]=nn;
for(int i=0;i<n;i++)
cout<<HT[i].key<<"\n";
}
void HashTable::withinsert(node nn)
{
int l=nn.key.length()%n;
if(HT[l].key=="#")
{
HT[l]=nn;
}
else
{
if(HT[l].key.length()%n==l)
{
int i;
for(i=(l+1)%n;HT[i].key!="#";i=(i+1)%n);
HT[i]=nn;
}
else
{
node temp;
temp=HT[l];
HT[l]=nn;
int i;
for(i=(l+1)%n;HT[i].key!="#";i=(i+1)%n);
HT[i]=temp;
}
}
for(int i=0;i<n;i++)
cout<<HT[i].key<<"\n";
}
void HashTable::create(){
string k,m;
int ch;
cout<<"\nEnter type of Linear Probing: \n1. Without Replacement\n2. With Replacement";
cin>>ch;
while(ch!=1 && ch!=2)
{
cout<<"\nInvalid!";
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 key: ";
cin>>nn.key;
cout<<"Enter meaning: ";
cin>>nn.meaning;
if(ch==1)
withoutinsert(nn);
if(ch==2)
withinsert(nn);
}
}
int main() {
int n;
cout<<"\nEnter number of dictionary entries: ";
cin>>n;
HashTable obj(n);
obj.create();
do{
cout<<"\nSELECT OPTION\n1. Retrieve\n2. Delete\n3. Exit";
cin>>n;
if(n==1)
obj.find();
if(n==2)
obj.del();
}
while(n!=3);
return 0;
}
/*
shreyas@shreyas:~/2416$ g++ A6.cpp
shreyas@shreyas:~/2416$ ./a.out
Enter number of dictionary entries: 4
Enter type of Linear Probing:
1. Without Replacement
2. With Replacement1
1 Entry
Enter key: 1
Enter meaning: a
#
1
#
#
2 Entry
Enter key: 2
Enter meaning: b
#
1
2
#
3 Entry
Enter key: 3
Enter meaning: ab
#
1
2
3
4 Entry
Enter key: 44
Enter meaning: lol
44
1
2
3
SELECT OPTION
1. Retrieve
2. Delete
3. Exit1
Enter key of element to be found4
NOT FOUND!
SELECT OPTION
1. Retrieve
2. Delete
3. Exit1
Enter key of element to be found44
FOUND!
44 - lol
SELECT OPTION
1. Retrieve
2. Delete
3. Exit2
Enter key of element to be deleted2
DELETED SUCCESSFULLY
SELECT OPTION
1. Retrieve
2. Delete
3. Exit3
shreyas@shreyas:~/2416$
*/