-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashing.cpp
195 lines (170 loc) · 3.86 KB
/
Hashing.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
#include<iostream>
#include<stdlib.h>
#include<string.h>
#define MAX 10
using namespace std;
struct Hash1
{
int id;
char name[20];
}rec[MAX] , tmp;
class Hashtable
{
public:
void initialize();
int hashing( int );
void woreplace(struct Hash1);
void wreplace(struct Hash1);
void place_at_next_slot(int , struct Hash1);
void display();
void search(int);
};
void Hashtable :: initialize()
{
int i;
for(i = 0; i < MAX; i++)
{
rec[i].id = -1;
strcpy( rec[i].name , "-");
}
}
int Hashtable :: hashing( int no )
{
int key;
key = no % MAX;
return(key);
}
void Hashtable :: woreplace(struct Hash1 h)
{
int i,k,z;
k = hashing(h.id);
if(rec[k].id == -1)
{
rec[k].id = h.id;
strcpy(rec[k].name , h.name);
}
else
place_at_next_slot(k,h);
}
void Hashtable :: wreplace(struct Hash1 h)
{
int k,p,change_pos;
k = hashing( h.id);
if(rec[k].id == -1)
{
rec[k].id = h.id;
strcpy(rec[k].name , h.name);
}
else if(hashing(rec[k].id) == hashing(h.id))
place_at_next_slot(k , h);
else
{
tmp.id = rec[k].id;
strcpy(tmp.name ,rec[k].name );
rec[k].id = h.id;
strcpy(rec[k].name , h.name);
place_at_next_slot(k , tmp);
}
}
void Hashtable :: place_at_next_slot(int k,struct Hash1 h)
{
int i,z;
for(i = 1;i < MAX;i++)
{
z = (k+i)%MAX;
if(rec[z].id == -1)
{
rec[z].id = h.id;
strcpy(rec[z].name , h.name);
break;
}
}
}
void Hashtable :: display()
{
cout<<"\n Collision Handling : \n\t---------#######---------";
cout<<"\n\t Index ID NAME \n\t----------------------\n";
for(int i=0;i<MAX;i++)
{
cout<<"\t"<<i<<" \t "<<rec[i].id;
cout<<"\t "<<rec[i].name;
cout<<"\n";
}
cout<<"\n\t------------------------";
}
void Hashtable :: search(int key)
{
int i,k,z;
int flag = 0;
k = hashing(key);
if(rec[k].id == key)
cout<<"\n key " <<key<< "found at home address || ";
else
{
for(z = 1;z < MAX;z++)
{
i = (k+z)%MAX;
if(rec[i].id == key)
{
cout<<"\n key " <<key<< "found at "<<i<<"address ||";
flag = 1;
break;
}
}
if(flag == 0)
cout<<"\n key "<<key<<"not found in table";
}
}
int main()
{
Hashtable h;
int ch,ch1,key;
char ans;
struct Hash1 temph;
do
{
cout<<"\n Collision Handling ";
cout<<"\n 1. without replacement \n 2. with replacement ";
cout<<"\n 3. Display Record \n 4.Search record \n 5.Exit \n Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
h.initialize();
do
{
cout<<"\n Enter ID : ";
cin>>temph.id;
cout<<"\n Enter Name : ";
cin>>temph.name;
h.woreplace(temph);
cout<<"\n Do you want to add more elements (0/1)? ";
cin>>ch1;
} while(ch1 != 0);
break;
case 2:
h.initialize();
do
{
cout<<"\n Enter ID : ";
cin>>temph.id;
cout<<"\n Enter NAME : ";
cin>>temph.name;
h.wreplace(temph);
cout<<"\n Do you want to add more elements (0/1)? ";
cin>>ch1;
}while(ch1 != 0);
break;
case 3:
h.display();
break;
case 4:
cout<<"\n Enter the id for searching : ";
cin>>key;
h.search(key);
break;
case 5 : exit(0);
}
}while( ch != 4 );
return 0;
}