-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
248 lines (173 loc) · 5.08 KB
/
HashTable.cpp
File metadata and controls
248 lines (173 loc) · 5.08 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
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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include <conio.h>
#include "Data.h"
#include "HashTable.h"
using namespace std;
HashTable * initHashTable (int sizeTable) {
//initializes the hashtable
HashTable * ht = new HashTable;
for(int i=1; i<=sizeTable; i++){
ht->elements[i]=NULL;
}
ht->sizeTable=sizeTable;
return ht;
}
HashTable * initHashTableFromFile (int sizeTable, char fileName[]) {
ifstream inputFile;
ofstream outputFile;
Movie movie;
int numMovies;
HashTable * ht = initHashTable(sizeTable);
inputFile.open(fileName);
if (!inputFile.is_open()) {
cout << "Input file " << fileName << " could not be opened. Aborting ..." << endl;
return ht;
}
numMovies = 0;
inputFile >> movie.ID;
while (movie.ID != "END") {
inputFile >> movie.yearReleased >> movie.duration >> movie.genre >> ws;
getline (inputFile, movie.title);
numMovies++;
insertHT (ht, movie);
inputFile >> movie.ID;
}
inputFile.close();
cout << "--> " << numMovies << " movies read from " << fileName << " and stored in hash table." << endl << endl;
return ht;
}
int toHash(string code, int sizeTable){
//function used to hash each movieID, returning the location in the HT
int value = 0;
int i = 2;
int location;
while (code[i]!='\0'){
value=value*10+(code[i]-'0');
i++;
}
location=value%sizeTable+1;
return location;
}
int containsHT (HashTable * ht, string key) {
int loc;
LLNode * curr;
loc=toHash(key, ht->sizeTable); //calls the hashing function to find the potential location in the HT
curr=ht->elements[loc]; //sets the top of the linked list using the location found
//search linked list until the data is found or until the list ends (not present)
while(curr!=NULL){
if(curr->data.ID==key){
return loc;
}
curr=curr->next;
}
return -1;
}
void displayMovie (Movie movie) {
cout << "ID: " << movie.ID << endl;
cout << "TITLE: " << movie.title << endl;
cout << "YEAR RELEASED: " << movie.yearReleased << endl;
cout << "DURATION: " << movie.duration << endl;
cout << "GENRE: " << movie.genre << endl;
cout << endl;
}
void displayMovieHT (HashTable * ht, string key) {
int location= containsHT(ht,key); //checking to see if the movie exists in the HT
LLNode * curr = ht->elements[location];
if(location!=-1){
while(curr!=NULL){
if(curr->data.ID==key){
displayMovie(curr->data); //displaying if it exists
}
curr=curr->next;
}
}
}
int lengthChain (HashTable * ht, int location) {
//if the top of the linked list is NULL, length is 0
if(ht->elements[location]==NULL){
return 0;
}
//linked list > 0, so set curr to the head of the list
LLNode * curr = ht->elements[location];
int num=0;
//finding the size of the linked list
while(curr!=NULL){
num++;
curr=curr->next;
}
return num;
}
LLNode * createNode (Movie movie) {
LLNode * newNode;
newNode = new LLNode;
newNode->data = movie;
newNode->next = NULL;
return newNode;
}
void insertHT (HashTable * ht, Movie movie) {
int loc;
LLNode * newNode;
string key=movie.ID;
loc=toHash(key, ht->sizeTable); //find location it would hash to
if(containsHT(ht,movie.ID)==-1){ // if the the HT doesn't contain the movie, create a newNode and insert in linked list
newNode = createNode(movie);
newNode->next = ht->elements[loc];
ht->elements[loc]= newNode;
}
}
void statisticsHT (HashTable * ht) {
int empty, notEmpty;
empty=0;
notEmpty=0;
int maxChain=INT_MIN;
for(int i=1; i<=ht->sizeTable; i++){
if(ht->elements[i]==NULL){
empty++; //counting the empty locations in HT (if the head is NULL)
}
else{
notEmpty++; //counting filled location
string key=ht->elements[i]->data.ID;
//finding the length of the chain and then find the max chain in HT
int length=lengthChain(ht,i);
if(length>maxChain){
maxChain=length;
}
}
}
//output the results after performing the necessary operations
cout << "Statistics on hash table: " << endl;
cout << "There are " << notEmpty << " filled locations." << endl;
cout << "There are " << empty << " empty locations. " << endl;
cout << "The length of the longest chain is: " << maxChain << endl;
}
void deleteHT (HashTable * ht, string key) {
//finding which linked list the key exists in, if it exists
int location= containsHT(ht,key);
LLNode * curr = ht->elements[location];
LLNode * pred = NULL;
//movie doesn't exist in HT
if(location==-1){
cout << "This movie does not exist." << endl;
}
//deleting the movie
else{
while(curr!=NULL){
if(curr->data.ID==key){
if(pred==NULL){
ht->elements[location]=ht->elements[location]->next;
}
else{
pred->next=curr->next;
}
delete curr;
return;
}
pred=curr;
curr=curr->next;
}
}
cout << "Movie has been deleted successfully." << endl;
}