-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash_Table_with_RLE.cpp
182 lines (180 loc) · 3.66 KB
/
Hash_Table_with_RLE.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
/*
Title : Program to implement Hash Table Using Linear Probing with and Without Replacement.
*/
#include <iostream>
#include <string.h>
using namespace std;
int MAX;
class hashing
{
struct student
{
char name[20];
long mobile;
int link;
student()
{
strcpy(name, "\0");
mobile = link = -1;
}
void accept()
{
cout << "\nEnter name : ";
cin >> name;
cout << "Enter mobile number : ";
cin >> mobile;
}
void display()
{
cout << name << "\t" << mobile << "\t" << link;
}
} * hashtable;
public:
hashing()
{
hashtable = new student[MAX];
for (int i = 0; i < MAX; i++)
{
hashtable[i].mobile = -1;
hashtable[i].link = -1;
}
}
int hash(long key)
{
int sum = 0;
for (; key > 0; key = key / 10)
sum += key % 10;
return (sum % MAX);
}
int empty(int loc)
{
int i = loc;
do
{
i++;
i = i % MAX;
} while (hashtable[i].mobile != -1 && i != loc);
return i;
}
void chaining_wo_replacement()
{
int i = 0, loc = -1, pos = -1, flag = 0;
char ans;
student s;
s.accept();
loc = hash(s.mobile);
if (hashtable[loc].mobile == -1)
hashtable[loc] = s;
else
{
pos = empty(loc);
if (pos == loc)
cout << "Hash table full\n";
else
{
if (hash(hashtable[loc].mobile) == hash(s.mobile))
{
i = loc;
while (hashtable[i].link != -1)
i = hashtable[i].link;
hashtable[pos] = s;
hashtable[i].link = pos;
}
else
{
for (i = loc + 1; (i % MAX) != loc; i++)
{
if (i == MAX)
i = 0;
if (hash(hashtable[i].mobile) == hash(s.mobile))
{
flag = 1;
break;
}
}
if (flag != 1)
hashtable[pos] = s;
else
{
while (hashtable[i].link != -1)
i = hashtable[i].link;
hashtable[pos] = s;
hashtable[i].link = pos;
}
}
}
}
display();
}
void chaining_w_replacement()
{
int i = 0, loc = -1, pos = -1, flag = 0;
char ans;
student s;
s.accept();
loc = hash(s.mobile);
if (hashtable[loc].mobile == -1)
hashtable[loc] = s;
else
{
pos = empty(loc);
if (pos == loc)
cout << "Hash table full\n";
else
{
if (hash(hashtable[loc].mobile) == hash(s.mobile))
{
i = loc;
while (hashtable[i].link != -1)
i = hashtable[i].link;
hashtable[pos] = s;
hashtable[i].link = pos;
}
else
{
int j;
i = hash(hashtable[loc].mobile);
while (i != loc)
{
j = i;
i = hashtable[i].link;
}
hashtable[pos] = hashtable[loc];
hashtable[j].link = pos;
hashtable[loc] = s;
}
}
}
display();
}
void display()
{
cout << "Hash key\tname\tmobile\tlink:";
for (int i = 0; i < MAX; i++)
{
cout << endl
<< i << " ";
hashtable[i].display();
}
}
};
int main()
{
for (int ch = 0, n = 0; ch < 3;)
{
cout << "\n1.Chaining without replacement \n2.Chaining with replacement \n3.Exit: ";
cin >> ch;
if (ch == 3)
continue;
cout << "Enter size of table: ";
cin >> MAX;
hashing h;
cout << "Enter the number of records: ";
cin >> n;
for (int i = 0; i < n; i++)
if (ch == 1)
h.chaining_wo_replacement();
else if (ch == 2)
h.chaining_w_replacement();
}
}