-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.cpp
292 lines (289 loc) · 5.47 KB
/
hashtable.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
#pragma
#include<iostream>
using namespace std;
template<class T>
class hash_node
{
T data;
hash_node<T>* nxt;
hash_node<T>* pre;
public:
hash_node<T>(T& data)
{
this->data = data;
nxt = NULL;
pre = NULL;
}
void set_data(T& data)
{
this->data = data;
}
void set_nxt (hash_node<T>* nxt)
{
this->nxt = nxt;
}
void set_pre (hash_node<T>* pre)
{
this->pre = pre;
}
T& get_data()
{
return data;
}
hash_node<T>* get_nxt()
{
return nxt;
}
hash_node<T>* get_pre()
{
return pre;
}
};
template<class T>
class HashTable
{
hash_node<T>** cable;
int size;
public:
HashTable(int size=50)
{
cable = new hash_node<T>*[size]();
for (int a = 0; a < size; a++)
{
cable[a] = NULL;
}
this->size = size;
}
int num(T& data)
{
int count = 0;
for (int a = 0; data.out_username()[a]!='\0'; a++)
{count += data.out_username()[a];
//cout<<arr[a]<<" ";
}
//cout<<endl;
return count;
}
int generator(T data)
{
return num(data) % size;
}
void insert(T& material)
{
hash_node<T>* neo = new hash_node<T>(material);
hash_node<T>* dam = cable[generator(material)];
// cout<<neo->get_data().out_username();
if (dam == NULL)
{
cable[generator(material)] = neo;
}
else
{
while (dam->get_nxt() != NULL)
{
dam = dam->get_nxt();
}
dam->set_nxt(neo);
neo->set_pre(dam);
}
}
int search_num(string data)
{
int count=0;
for(int a=0;data[a]!='\0';a++)
{
count+=data[a];
}
return count%size;
}
hash_node<T>* hash_search(T data)
{
hash_node<T>* dam = cable[generator(data)];
if (cable == NULL)
{
cout << "the cable is empty " << endl;
return NULL;
}
else
{
if (dam == NULL)
{
cout << "the index is empty" << endl;
return NULL;
}
else
{
while (dam != NULL)
{
if (data.out_username() == dam->get_data().out_username())
{
return dam;
}
dam = dam->get_nxt();
}
cout << "the data not found" << endl;
return NULL;
}
}
}
hash_node<T>* account_search(string data)
{
hash_node<T>* dam = cable[search_num(data)];
if (cable == NULL)
{
// cout << "the cable is empty " << endl;
return NULL;
}
else
{
if (dam == NULL)
{
// cout << "the data not found : " <<search_num(data)<< endl;
return NULL;
}
else
{
while (dam != NULL)
{
if (data == dam->get_data().out_username())
{
return dam;
}
dam = dam->get_nxt();
}
// cout << "the data not found" << endl;
return NULL;
}
}
}
hash_node<T>* last_node (hash_node<T>* hash_node)
{
hash_node->get_pre()->set_nxt(NULL);
return hash_node;
}
hash_node<T>* mid_node (hash_node<T>* hash_node)
{
hash_node->get_pre()->set_nxt(hash_node->get_nxt());
hash_node->get_nxt()->set_pre(hash_node->get_pre());
return hash_node;
}
hash_node<T>* first_node (hash_node<T>* hash_node)
{
return hash_node->get_nxt();
}
bool remove(T data)
{
if (cable == NULL)
{
cout << "the cable is empty" << endl;
return false;
}
else
{
hash_node<T>* dam = cable[generator(data)];
if (dam == NULL)
{
cout << "the index is empty :" <<cable[6]<< endl;
return false;
}
while (dam!= NULL)
{
if (data == dam->get_data())
{
if (dam->get_nxt() == NULL&&dam->get_pre()!=NULL)
{
delete dam;
dam->get_pre()->set_nxt(NULL);
return true;
}
else if (dam->get_pre() == NULL&&dam->get_nxt()!=NULL)
{
delete dam;
//delete cable[generator(data)];
cable[generator(data)]=dam->get_nxt();
//delete first_node(dam);
return true;
}
else if(dam->get_pre()==NULL&&dam->get_nxt()==NULL)
{
delete dam;
//delete cable[generator(data)];
cable[generator(data)]=NULL;
// cout << dam->get_data() << endl;
return true;
}
else
{
dam->get_pre()->set_nxt(dam->get_nxt());
dam->get_nxt()->set_pre(dam->get_pre());
delete dam;
return true;
}
return dam;
}
dam = dam->get_nxt();
}
cout << "the data not found " << endl;
return false;
}
}
void traverse()
{
if (cable == NULL)
{
cout << "empty cable " << endl;
}
else
{
for (int a = 0; a < size; a++)
{
hash_node<T>* mover = cable[a];
while (mover != NULL)
{
cout <<mover->get_data()<<"->"<<endl;
mover = mover->get_nxt();
}
}
}
cout << endl;
}
void write_hash(ofstream& fout)
{
if (cable == NULL)
{
cout << "empty cable " << endl;
}
else
{
for (int a = 0; a < size; a++)
{
hash_node<T>* mover = cable[a];
while (mover != NULL)
{
fout << mover->get_data();
mover = mover->get_nxt();
}
}
}
cout << endl;
}
};
// int main()
// {
// HashTable<string> obj(7);
// obj.insert("12/04/2022-qawwalli day");
// obj.insert("11/02/2023-murre trip");
// obj.insert("01/07/2022-speech");
// obj.insert("11/04/2022-urs");
// obj.insert("11/04/2023-kwjQJ");
// obj.insert("10/04/2022-Iehfiuwgfbkj");
// obj.insert("02/07/2022-jdiWFHIUFA");
// obj.traverse();
// if (obj.remove("11/04/2022-urs"))
// {
// cout << "deleted " << endl;
// }
// obj.traverse();
// //cout<<obj.search("ali@gmail.com")->get_data()<<endl;
// return 0;
// }