-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashing.cpp
More file actions
283 lines (266 loc) · 6.14 KB
/
Hashing.cpp
File metadata and controls
283 lines (266 loc) · 6.14 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
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
/*
Name: Chirantan Joshi.
Div: G
Batch: G1
Roll No.: PG13
STUDENT RECORDS BY HASHING
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
#define max 10
class student
{
int rollno; //Different fields for student
string name;
string grade;
public:
student() //initializing the fields
{
rollno=-1;
name="-";
grade="-";
}
friend class hashs;
};
class hashs
{
student hashtable[max];
int i;
public:
hashs() //Making all roll no. entries in hashtable as -1
{
for(i=0;i<max;i++)
{
hashtable[i].rollno=-1;
}
}
void display() //Displaying hash table
{
cout<<"\nINDEX\tROLL_NO\tNAME\tGRADE\n";
for(i=0;i<max;i++)
{
cout<<i<<"\t";
cout<<hashtable[i].rollno<<"\t";
cout<<hashtable[i].name<<"\t ";
cout<<hashtable[i].grade<<"\n";
}
}
void linear_wor() //linear probing without replacement
{
student s;
char ch='y';
int index=0;
while(ch=='y')
{
cout<<"Enter the roll no.: "; //Accepting the student attributes
cin>>s.rollno;
cout<<"Enter the Name: ";
cin>>s.name;
cout<<"Enter the Grade: ";
cin>>s.grade;
index=s.rollno%max;
if(hashtable[index].rollno==-1) //Checking if roll no. in hash table is -1
hashtable[index]=s; //if yes add student directly in table
else //else
{
i=1; //add student at next empty location in table
i=(index+i)%max;
while(i!=index)
{
if(hashtable[i].rollno==-1)
{
hashtable[i]=s;
break;
}
i=(i+1)%max;
}
if(i==index) //if all the locations are filled,warn user
cout<<endl<<"Hash is full!!"<<endl<<endl;
}
cout<<"Do you want to add more entries(y/n)? ";
cin>>ch;
}
display(); //display the table
}
void linear_wr() //linear probing with replacement
{
student s,temp;
char ch='y';
int index=0;
while(ch=='y')
{
cout<<"Enter the roll no.: "; //Accept the attributes of student
cin>>s.rollno;
cout<<"Enter the Name: ";
cin>>s.name;
cout<<"Enter the Grade: ";
cin>>s.grade;
index=s.rollno%max;
if(hashtable[index].rollno==-1) //Checking if roll no. in hash table is -1
hashtable[index]=s; //if yes add student directly in table
else //else
{
temp=s; //store the attributes at a temporary variable
if(index!=hashtable[index].rollno%max) //if the location is already filled
{
temp=hashtable[index]; //store the attribute stored at that location in temp
hashtable[index]=s; //store the attributes of new student at that location
}
i=(index+1)%max;
while(i!=index) //store that temp at next empty location in table
{
if(hashtable[i].rollno==-1)
{
hashtable[i]=temp;
break;
}
i=(i+1)%max;
}
if(i==index) //if all the locations are filled,warn user
cout<<endl<<"Hash is full!!"<<endl<<endl;
}
cout<<"Do you want to add more entries(y/n)? ";
cin>>ch;
}
display(); //Display hash table
}
};
int main()
{
hashs h1,h2;
int val,choice;
while(1)
{
cout<<"\n------------------MENU------------------\n\n1.Linear Probing without replacement.\n2.Linear Probing with replacement.\n3.Exit.\nEnter the choice: ";
cin>>choice; //Taking choice for different operations
switch(choice)
{
case 1: h1.linear_wor();
break;
case 2: h2.linear_wr();
break;
case 3: exit(1);
break;
default:cout<<"Wrong choice!!\nEnter the valid choice.\n";
}
}
return 0;
}
/*
OUTPUT:
------------------MENU------------------
1.Linear Probing without replacement.
2.Linear Probing with replacement.
3.Exit.
Enter the choice: 1
Enter the roll no.: 1
Enter the Name: Ram
Enter the Grade: A+
Do you want to add more entries(y/n)? y
Enter the roll no.: 11
Enter the Name: Raghav
Enter the Grade: A
Do you want to add more entries(y/n)? y
Enter the roll no.: 22
Enter the Name: Krishna
Enter the Grade: B
Do you want to add more entries(y/n)? y
Enter the roll no.: 45
Enter the Name: Vishnu
Enter the Grade: B+
Do you want to add more entries(y/n)? n
INDEX ROLL_NO NAME GRADE
0 -1 - -
1 1 Ram A+
2 11 Raghav A
3 22 Krishna B
4 -1 - -
5 45 Vishnu B+
6 -1 - -
7 -1 - -
8 -1 - -
9 -1 - -
------------------MENU------------------
1.Linear Probing without replacement.
2.Linear Probing with replacement.
3.Exit.
Enter the choice: 2
Enter the roll no.: 1
Enter the Name: Ram
Enter the Grade: A+
Do you want to add more entries(y/n)? y
Enter the roll no.: 11
Enter the Name: Raghav
Enter the Grade: A
Do you want to add more entries(y/n)? y
Enter the roll no.: 10
Enter the Name: Shyam
Enter the Grade: B
Do you want to add more entries(y/n)? y
Enter the roll no.: 22
Enter the Name: Vishnu
Enter the Grade: B+
Do you want to add more entries(y/n)? y
Enter the roll no.: 20
Enter the Name: Krishna
Enter the Grade: A
Do you want to add more entries(y/n)? y
Enter the roll no.: 35
Enter the Name: Raghu
Enter the Grade: B
Do you want to add more entries(y/n)? y
Enter the roll no.: 24
Enter the Name: Samar
Enter the Grade: F
Do you want to add more entries(y/n)? n
INDEX ROLL_NO NAME GRADE
0 10 Shyam B
1 1 Ram A+
2 22 Vishnu B+
3 11 Raghav A
4 24 Samar F
5 35 Raghu B
6 20 Krishna A
7 -1 - -
8 -1 - -
9 -1 - -
------------------MENU------------------
1.Linear Probing without replacement.
2.Linear Probing with replacement.
3.Exit.
Enter the choice: 2
Enter the roll no.: 17
Enter the Name: Somnath
Enter the Grade: C+
Do you want to add more entries(y/n)? y
Enter the roll no.: 26
Enter the Name: Ramraj
Enter the Grade: C
Do you want to add more entries(y/n)? y
Enter the roll no.: 18
Enter the Name: Ganesh
Enter the Grade: D
Do you want to add more entries(y/n)? y
Enter the roll no.: 19
Enter the Name: Raja
Enter the Grade: D
Hash is full!!
Do you want to add more entries(y/n)? n
INDEX ROLL_NO NAME GRADE
0 10 Shyam B
1 1 Ram A+
2 22 Vishnu B+
3 11 Raghav A
4 24 Samar F
5 35 Raghu B
6 26 Ramraj C
7 17 Somnath C+
8 18 Ganesh D
9 19 Raja D
------------------MENU------------------
1.Linear Probing without replacement.
2.Linear Probing with replacement.
3.Exit.
Enter the choice: 3
*/