-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
268 lines (234 loc) · 6.08 KB
/
hash.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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class h
{
private:
static const int tableSize = 5; //sets table size
struct item //defines each "array cell"
{
string name;
string drink;
item* next;
};
item* HashTable[tableSize];
public:
h();
int Hash(string key); // determines index
void AddItem(string name,string drink);
int NumberOfItemsInIndex(int index);
void PrintTable();
void PrintItemsInIndex(int index);
void FindDrink(string name);
void RemoveItem(string name);
};
h::h() //constructor
{
for(int i = 0; i < tableSize; i++)
{
HashTable[i] = new item;
HashTable[i]-> name = "empty";
HashTable[i]-> drink = "empty";
HashTable[i]-> next = NULL;
}
}
void h::AddItem(string name, string drink)
{
int index = Hash(name);
if(HashTable[index]-> name == "empty")
{
HashTable[index]-> name = name;
HashTable[index]-> drink = drink;
}
else
{
item* Ptr = HashTable[index];
item* n = new item;
n-> name = name;
n-> drink = drink;
n-> next = NULL;
while(Ptr-> next != NULL)
{
Ptr = Ptr-> next;
}
Ptr-> next = n;
}
}
int h::NumberOfItemsInIndex(int index) // check to see how many items in bucket
{
int count = 0;
if(HashTable[index]-> name == "empty")
{
return count;
}
else
{
count++;
item* Ptr = HashTable[index];
while(Ptr-> next != NULL)
{
count++;
Ptr = Ptr-> next;
}
}
return count;
}
void h::PrintTable()
{
int number;
for(int i = 0; i < tableSize; i++)
{
number = NumberOfItemsInIndex(i);
cout << "---------------------\n";
cout << "index = " << i << endl;
cout << HashTable[i]-> name << endl;
cout << HashTable[i]-> drink << endl;
cout << "# of items = " << number << endl;
cout << "---------------------\n";
}
}
void h::PrintItemsInIndex(int index)
{
item* Ptr = HashTable[index];//check just this bucket or index
if(Ptr-> name == "empty")
{
cout << "bucket " << index << " is empty";
cout << endl;
}
else
{
cout << "bucket " << index << " contains the following item\n";
while(Ptr != NULL) // as long as bucket is not empty
{
cout << "--------------------\n";
cout << Ptr-> name << endl;
cout << Ptr-> drink << endl;
cout << "--------------------\n";
Ptr = Ptr-> next; // check for next name
}
}
}
void h::FindDrink(string name)
{
int bucket = Hash(name);
bool foundName = false;
string drink;
item* Ptr = HashTable[bucket];//bucket is the first name
while(Ptr != NULL) //as long as bucket is not empty
{
if(Ptr-> name == name)
{
foundName = true;//easy enough
drink = Ptr-> drink;
}
Ptr = Ptr-> next; //check next
}
if(foundName == true)
{
cout << "Favorite drink = " << drink << endl;
}
else
{
cout << name << "'s info was not found in the Hash Table\n";
}
}
void h::RemoveItem(string name)
{
int index = Hash(name);
item* delPtr;
item* P1;
item* P2;
//case 0 - bucket is empty
if(HashTable[index]-> name == "empty" && HashTable[index]-> drink == "empty")
{
cout << name << " was not found in the Hash Table\n";
}
//case 1 - only 1 item in bucket & item has matching name
else if(HashTable[index]-> name == name && HashTable[index]-> next == NULL)
{
HashTable[index]-> name = "empty";
HashTable[index]-> drink = "empty";
cout << name << " was removed from Hash Table\n";
}
//case 2 - match 1st item in bucket but there more items in bucket
else if(HashTable[index]-> name == name)
{
delPtr = HashTable[index];
HashTable[index] = HashTable[index]-> next;
delete delPtr;
cout << name << " was removed from Hash Table\n";
}
//case 3 - bucket contains items but 1st item not a match
else
{
P1 = HashTable[index]-> next;
P2 = HashTable[index];
while(P1 != NULL && P1-> name != name)
{
P2 = P1;
P1 = P1-> next;
}
//case 3.1 - no match
if(P1 == NULL)
{
cout << name << " was not found in the Hash Table\n";
}
//case 3.2 - match is found after 1st item
else
{
delPtr = P1;
P1 = P1-> next;
P2-> next = P1;
delete delPtr;
cout << name << " was removed from Hash Table\n";
}
}
}
int h::Hash(string key)
{
int hash = 0;
int index;
for(int i = 0; i < key.length(); i++)
{
hash = hash + (int)key[i];
}
index = hash % tableSize; //Modulo - Remainder comes index
return index;
}
int main() {
h Hashy;
string name = "";
Hashy.AddItem("Jack", "Balvenie");
Hashy.AddItem("Dan", "McCallan");
Hashy.AddItem("Randy", "Soju");
Hashy.AddItem("Hugh", "Heineken");
Hashy.AddItem("Rose", "Chardonnay");
Hashy.AddItem("Kay", "Merlot");
Hashy.AddItem("Sophia", "POG");
Hashy.AddItem("Isabelle", "Water");
Hashy.AddItem("Oreo", "Milk");
Hashy.AddItem("Mother", "Ginger Ale");
Hashy.PrintTable();
//Hashy.PrintItemsInIndex(4); //to find who is in each bucket where there is more than one
/*while(name != "exit")//To look for someone's favorite drink
{
cout << "Search for: ";
cin >> name;
if(name != "exit")
{
Hashy.FindDrink(name);
}
}*/
while(name != "exit")//To look for someone's favorite drink
{
cout << "Remove: ";
cin >> name;
if(name != "exit")
{
Hashy.RemoveItem(name);
}
}
//Hashy.PrintTable();
return 0;
}