-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableVoid.cc
More file actions
131 lines (122 loc) · 2.87 KB
/
HashTableVoid.cc
File metadata and controls
131 lines (122 loc) · 2.87 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
//
// Implementation of a HashTable that stores void *
//
#include "HashTableVoid.h"
// Obtain the hash code of a key
int HashTableVoid::hash(const char * key)
{
// Add implementation here
int h = 0;
const char * p = key;
while (*p) {
h += *p;
p++;
}
return h % TableSize;
}
// Constructor for hash table. Initializes hash table
HashTableVoid::HashTableVoid()
{
// Add implementation here
_buckets = (HashTableVoidEntry **) malloc (TableSize*sizeof(HashTableVoidEntry *));
for (int i=0; i < TableSize; i++) {
_buckets[i] = NULL;
}
}
// Add a record to the hash table. Returns true if key already exists.
// Substitute content if key already exists.
bool HashTableVoid::insertItem( const char * key, void * data)
{
// Add implementation here
int h = hash(key);
HashTableVoidEntry * e = _buckets[h];
while (e != NULL) {
if (!strcmp(e->_key, key)) {
// Entry found
e->_data = data;
return true;
}
e = e->_next;
}
// Entry not found. Add it.
e = new HashTableVoidEntry;
e->_key = strdup(key);
e->_data = data;
e->_next = _buckets[h];
_buckets[h] = e;
return false;
}
// Find a key in the dictionary and place in "data" the corresponding record
// Returns false if key is does not exist
bool HashTableVoid::find( const char * key, void ** data)
{
// Add implementation here
int h = hash(key);
HashTableVoidEntry * e = _buckets[h];
while (e != NULL) {
if (!strcmp(e->_key, key)) {
// Entry found
*data = e->_data;
return true;
}
e = e->_next;
}
return false;
}
// Removes an element in the hash table. Return false if key does not exist.
bool HashTableVoid::removeElement(const char * key)
{
// Add implementation here
int h = hash(key);
HashTableVoidEntry * e = _buckets[h];
HashTableVoidEntry * prev = NULL;
while (e != NULL) {
if (!strcmp(e->_key, key)) {
// Entry found
if (prev != NULL) {
prev->_next = e->_next;
}
else {
_buckets[h] = e->_next;
}
// free(e->_key);
delete e;
return true;
}
prev = e;
e = e->_next;
}
return false;
}
// Creates an iterator object for this hash table
HashTableVoidIterator::HashTableVoidIterator(HashTableVoid * hashTable)
{
// Add implementation here
_currentBucket = 0;
_currentEntry = NULL;
_hashTable = hashTable;
}
// Returns true if there is a next element. Stores data value in data.
bool HashTableVoidIterator::next(const char * & key, void * & data)
{
// Add implementation here
if (_currentEntry != NULL) {
_currentEntry = _currentEntry->_next;
if (_currentEntry != NULL) {
key = _currentEntry->_key;
data = _currentEntry->_data;
return true;
}
}
_currentBucket++;
while (_currentBucket < HashTableVoid::TableSize) {
if (_hashTable->_buckets[_currentBucket] != NULL) {
_currentEntry = _hashTable->_buckets[_currentBucket];
key = _currentEntry->_key;
data = _currentEntry->_data;
return true;
}
_currentBucket++;
}
return false;
}