-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
219 lines (174 loc) · 3.6 KB
/
hash.cpp
File metadata and controls
219 lines (174 loc) · 3.6 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
#include "stdafx.h"
#include <iostream>
#include <string>
#include "hash.h"
using namespace std;
hashT::hashT(){
for(int i =0; i<tableSize ;i++)
{
hashTable[i] = new item;
hashTable[i]->name = "empty";
hashTable[i]->drink = "empty";
hashTable[i]->next = NULL;
}
}
void hashT::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;
}
}
void hashT::PrintTable(){
int number;
for (int i = 0; i < tableSize; i++)
{
number = NumberOfItemsInIndex(i);
cout<<"-----------------------------"<<endl;
cout<<"index = "<<i<<endl;
cout<<hashTable[i]->name<<endl;
cout<<hashTable[i]->drink<<endl;
cout<<"# of items = "<<number<<endl;
cout<<"------------------------------"<<endl;
}
}
void hashT::PrintItemsInIndex(int index)
{
item* ptr = hashTable[index];
if (ptr->name == "empty")
{
cout<<"index = "<<index <<" is empty"<<endl;
}
else
{
cout<<"index = "<<index<<" contains the following items"<<endl;
while (ptr != NULL)
{
cout<<"------------------------------"<<endl;
cout<<ptr->name <<endl;
cout<<ptr->drink<<endl;
cout<<"--------------------------------"<<endl;
ptr = ptr->next;
}
}
}
int hashT::NumberOfItemsInIndex(int index)
{
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;
}
}
int hashT::Hash (string key)
{
int hash = 0;
int index;
for(int i =0; i<key.length(); i++)
{
hash = hash +(int)key[i];
}
index = hash % tableSize;
return index;
}
void hashT::FindDrink(string name)
{
int index = Hash(name);
bool foundName = false;
string drink;
item* ptr = hashTable[index];
while (ptr != NULL)
{
if(ptr->name == name)
{
foundName = true;
drink = ptr->drink;
}
ptr = ptr->next;
}
if(foundName == true)
{
cout<<"Favourite drink = "<<drink<<endl;
}
else
{
cout<<name<<"'s info was not found in the hash table"<<endl;
}
}
void hashT::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 table"<<endl;
}
//case 1 - only 1 item in the bucket and that is the item we are looking for
else if(hashTable[index]->name == name && hashTable[index]->next == NULL)
{
hashTable[index]->name = "empty";
hashTable[index]->drink = "empty";
cout<< name <<" was removed from the table"<<endl;
}
//case 2 - match is located in the first item but there are more items in the bucket
else if(hashTable[index]->name == name)
{
delPtr = hashTable[index];
hashTable[index] = hashTable[index]->next;
delete delPtr;
cout<< name <<" was removed from the table"<<endl;
}
//case 3 - bucket contains the item but first item is not the 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 table"<<endl;
}
//case 3.2 - match is found
else
{
delPtr = P1;
P1 = P1->next;
P2->next = P1;
delete delPtr;
cout<< name <<" was removed from the table"<<endl;
}
}
}