-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.cpp
147 lines (133 loc) · 4.32 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
// ------------------------------------------------ HashTable.cpp -------------------------------------------------------
// Jasdeep Brar, Cameron Ufland CSS343 C
// Creation Date: March 1, 2020
// Date of Last Modification: March 14, 2020
// --------------------------------------------------------------------------------------------------------------------
// This is the implementation file for the HashTable class.
// --------------------------------------------------------------------------------------------------------------------
//The requirements for this assignment were specified by Wooyoung Kim via class
// and canvas.
// --------------------------------------------------------------------------------------------------------------------
#include "HashTable.h"
//constructor
HashTable::HashTable() {
this->size = 0;
this->capacity = 10;
this->table = new Node * [10]();
}
//end constructor
//destructor
HashTable::~HashTable()
{
this->clear();
}
//end destructor
// -------------------------------- insert() --------------------------------
// Description
// insert: inserts a new customer object into the hash table
// preconditions:valid customer object, valid hashtable object
// postconditions: customer object is inserted into the hashtable
// -----------------------------------------------------------------------------
bool HashTable::insert(Customer *cust)
{
if (!(this->contains(cust)))
{
int index = hash(cust->getCustomerID());
Node *curr = new Node(*cust);
curr->next = table[index];
table[index] = curr;
size++;
return true;
}
return false;
}
//end insert
// -------------------------------- retrieve() --------------------------------
// Description
// retrieve: finds the specified customer object and stores it in a provided pointer
// preconditions: empty pointer, a key value
// postconditions: boolean, pointer with specified customer value or nullptr
// -----------------------------------------------------------------------------
bool HashTable::retrieve(int key, Customer *&holder)
{
int index = hash(key);
Node* current = table[index];
while (current != NULL)
{
if (current->data.customerID == key)
{
holder = ¤t->data;
return true;
}
current = current->next;
}
return false;
}
//end retrieve
// -------------------------------- contains() --------------------------------
// Description
// containse: checks whether the customer object is in the hashtable based on the provided customer object
// preconditions:valid customer object
// postconditions: the customer object has been shown to be in the hashtable
// -----------------------------------------------------------------------------
bool HashTable::contains(Customer *cust)
{
int index = hash(cust->getCustomerID());
Node *curr = table[index];
while (curr != nullptr) {
if (curr->data == *cust){
return true;
}
curr = curr->next;
}
return false;
}
//end contains
// -------------------------------- contains() --------------------------------
// Description
// containse: checks whether the customer object is in the hashtable based on a provided key
// preconditions:key
// postconditions: the customer object has been shown to be in the hashtable
// -----------------------------------------------------------------------------
bool HashTable::contains(int key) {
int index = hash(key);
Node *curr = table[index];
while (curr != nullptr) {
if (curr->data.getCustomerID() == key) {
return true;
}
curr = curr->next;
}
return false;
}
// end contains
// -------------------------------- clear() --------------------------------
// Description
// clear:deletes the contents of the hashtable
// preconditions: a valid hashtable object
// postconditions: hashtable is deleted
// -----------------------------------------------------------------------------
void HashTable::clear(){
for(int i = 0; i < capacity; i++){
Node *curr = table[i];
while(curr != nullptr){
Node *temp = curr;
curr = curr->next;
delete temp;
temp = nullptr;
}
}
delete[] table;
}
//end clear
// -------------------------------- hash() --------------------------------
// Description
// hash: creates a hash coded based on the provided int
// preconditions:none
// postconditions: none
// -----------------------------------------------------------------------------
int HashTable::hash(int key)
{
return key % capacity;
}
//end hash