-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathhashTable.h
129 lines (98 loc) · 3.48 KB
/
hashTable.h
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
//*****************************************************************************************************
//
// This header file defines a class template for a hash table with quadratic probing to resolve
// collisions when inserting and removing items.
//
// Other files required:
// 1. htElement.h - header file for the HTElement struct
//
//*****************************************************************************************************
#ifndef HASHTABLE_H
#define HASHTABLE_H
//*****************************************************************************************************
#include "htElement.h"
#include <iostream>
//*****************************************************************************************************
template <typename T>
class HashTable {
private:
HTElement<T> *table;
int size;
int count;
int _hash(const T &item) const;
int _probe(const T &item) const;
public:
HashTable(int size = 10);
~HashTable();
void remove(const T &item);
void insert(const T &item);
void display() const;
T *search(const T &item) const;
};
//*****************************************************************************************************
template <typename T>
HashTable<T>::HashTable(int size) {
table = new HTElement<T>[size];
this->size = size;
count = 0;
}
//*****************************************************************************************************
template <typename T>
HashTable<T>::~HashTable() {
delete[] table;
table = nullptr;
}
//*****************************************************************************************************
template <typename T>
int HashTable<T>::_hash(const T &item) const {
return item.hash(size);
}
//*****************************************************************************************************
template <typename T>
int HashTable<T>::_probe(const T &item) const {
int index = _hash(item);
int hashIndex = index;
int i = 0;
while ((table[index].status == 1 || table[index].status == -1) && (table[index].item != item)) {
index = (hashIndex + (i * i)) % size;
++i;
}
return index;
}
//*****************************************************************************************************
template <typename T>
void HashTable<T>::remove(const T &item) {
int index = _probe(item);
if (table[index].status == 1) {
table[index].status = -1;
--count;
}
}
//*****************************************************************************************************
template <typename T>
void HashTable<T>::insert(const T &item) {
int index = _probe(item);
if (table[index].status != 1) {
table[index].item = item;
table[index].status = 1;
count++;
}
}
//*****************************************************************************************************
template <typename T>
void HashTable<T>::display() const {
for (int i = 0; i < size; ++i)
if (table[i].status == 1)
std::cout << table[i].item << std::endl;
}
//*****************************************************************************************************
template <typename T>
T *HashTable<T>::search(const T &item) const {
int index = _probe(item);
T *copy = nullptr;
if (table[index].status == 1)
copy = new T(table[index].item);
return copy;
}
//*****************************************************************************************************
#endif