-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.cpp
More file actions
193 lines (168 loc) · 4.76 KB
/
hash_table.cpp
File metadata and controls
193 lines (168 loc) · 4.76 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
//Tim Garvin
#include "hash_table.h"
//Constructor implementation (Initialization of variables)
Hash_Table::Hash_Table()
{
size = 10;
Element* initial_element = new Element(); //Initializes a new class Element object to initialize the hash table elements
initial_element->empty = true;
//Initializes the hash table elements with initial_element
for(int i = 0; i < size; i++)
{
table_data[i] = initial_element;
}
}
//Adds a new element to the hash table
void Hash_Table::add(string fName, string lName, string id, string class_level, string mjr)
{
//Checks that the hash table isn't full nor that the key already exists before adding a new element
if(!check_key_exists(atoi(id.c_str())) && !check_full())
{
//Creates a new Element class object
Element* new_element = new Element();
//Creates a new Student class object
Student *new_student;
new_student = new Student();
//Sets values in the Student class object to add to the Hash Table
new_student->firstName = fName;
new_student->lastName = lName;
new_student->studentID = id;
new_student->classification = class_level;
new_student->major = mjr;
//Sets values in the Element class object to add to the Hash Table
new_element->key = atoi(id.c_str());
new_element->data = new_student;
new_element->empty = false;
int current_key = new_element->get_key();
int index = 0;
//Calculates the value from the hash table function to determine the index for where to place the new element in the hash table
index = current_key % size;
//Checks if the calculated index is empty, uses a rehashing function to find a new index if it isn't
if(table_data[index]->check_empty() == true)
{
table_data[index] = new_element; //Adds the new element to the hash table
}
else
{
int rehash_result = 0;
//Loops to find a new element index
for(int i = 1; i < size; i++)
{
rehash_result = (index + i*i)%size; //Rehash function to find a new index in the hash table
//Checks if the newly calculated index is empty
if(table_data[rehash_result]->check_empty() == true)
{
table_data[rehash_result] = new_element; //Adds the new element to the hash table
break;
}
}
}
}
}
//Removes an element from the hash table by clearing values and marking as deleted
void Hash_Table::remove(string id)
{
//Loops to find the key and remove it if the element exists
for(int i = 0; i < size; i++)
{
//Removes the element if it exists
if(table_data[i]->get_key() == atoi(id.c_str()))
{
table_data[i]->key = -1;
table_data[i]->data->firstName = "Deleted";
table_data[i]->data->lastName = "Deleted";
table_data[i]->data->studentID = "Deleted";
table_data[i]->data->classification = "Deleted";
table_data[i]->data->major = "Deleted";
table_data[i]->empty = true;
table_data[i]->deleted = true;
cout<<"true"<<endl;
break;
}
else if(i == size-1)
{
cout<<"false"<<endl; //Indicates to the user that the key does not exist
}
}
}
//Prints all elements to the user
void Hash_Table::print()
{
//Loops through the hash table to print all elements
for(int i = 0; i < size; i++)
{
//Prints the element when the current index is not empty in the hash table
if(table_data[i]->check_empty() == false)
{
cout<<"("<<table_data[i]->data->firstName<<",";
cout<<table_data[i]->data->lastName<<",";
cout<<table_data[i]->data->studentID<<",";
cout<<table_data[i]->data->classification<<",";
cout<<table_data[i]->data->major<<")";
}
else if(table_data[i]->check_deleted() == true)
{
cout<<"(DEL)"; //Prints (DEL) when the current index was deleted from the hash table
}
else
{
cout<<"(NULL)"; //Prints (NULL) when the current index was never assigned an element in the hash table
}
//Ends the line for the display when the end of the hash table is reached
if(i == size-1)
{
cout<<endl;
}
}
}
//Checks to make sure the key doesn't already exist
bool Hash_Table::check_key_exists(int id)
{
bool keyExists = false;
//Loops to see if the key already exists in the hash table
for(int i = 0; i < size; i++)
{
if(table_data[i]->get_key() == id)
{
keyExists = true;
}
}
//Returns true if key exists, false if it doesn't
if(keyExists)
{
cout<<"Error! Student ID already exists!"<<endl;
return true;
}
else
{
return false;
}
}
//Checks to make sure the hash table isn't already full
bool Hash_Table::check_full()
{
bool full = true;
//Loops through the hash table to see if it is full
for(int i = 0; i < size; i++)
{
if(table_data[i]->check_empty() == true)
{
full = false;
}
}
//Returns true if full, false if not full
if(full)
{
cout<<"Error! Hash table is full!"<<endl;
return true;
}
else
{
return false;
}
}
//Destructor
Hash_Table::~Hash_Table()
{
delete [] table_data;
}