Skip to content

Commit 97ec67b

Browse files
committed
Add Simple example: Hash Table
1 parent 0648e96 commit 97ec67b

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

DataStructure/Hash/example.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* example.cpp
3+
* Copyright (C) 2019 Guowei Chen <[email protected]>
4+
*
5+
* Distributed under terms of the MIT license.
6+
*/
7+
8+
9+
#include <iostream>
10+
#include <cstdlib>
11+
#include <ctime>
12+
#include "hash_table.hpp"
13+
14+
constexpr int N = 48;
15+
constexpr int MAX_VALUE = 1000;
16+
17+
int main()
18+
{
19+
srand(time(nullptr));
20+
21+
hash_table table;
22+
23+
// TEST: insert.
24+
for (int i = 0; i < N; ++i) {
25+
pRecord r = (pRecord) malloc(sizeof(Record));
26+
27+
r->key = rand() % MAX_VALUE;
28+
r->value = rand() % MAX_VALUE;
29+
30+
if (table.search(r->key) != nullptr) {
31+
--i;
32+
continue;
33+
}
34+
35+
std::cout << "Inserting: Key => " << r->key << ", Value =>" << r->value << "\n";
36+
table.insert(r);
37+
}
38+
39+
// TEST: search & remove.
40+
pRecord tmp;
41+
for (int i = 0; i < MAX_VALUE; ++i) {
42+
if ((tmp = table.search(i)) != nullptr) {
43+
std::cout << "Deleting: Key => " << tmp->key << ", Value => " << tmp->value << "\n";
44+
table.remove(tmp->key);
45+
}
46+
}
47+
48+
return 0;
49+
}

DataStructure/Hash/hash_table.hpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* hash_table.hpp
3+
* Copyright (C) 2019 Guowei Chen <[email protected]>
4+
*
5+
* Distributed under terms of the MIT license.
6+
*/
7+
8+
#ifndef _HASH_TABLE_HPP_
9+
#define _HASH_TABLE_HPP_
10+
11+
struct Record {
12+
int key;
13+
int value;
14+
};
15+
16+
typedef Record * pRecord;
17+
18+
class hash_table {
19+
public:
20+
hash_table() {
21+
for (int i = 0; i < MAX_SIZE; ++i) {
22+
table[i] = nullptr;
23+
}
24+
}
25+
26+
// TODO: release the memory.
27+
~hash_table() {}
28+
29+
// NOTE: insert operation.
30+
void insert(pRecord r) {
31+
if (r == nullptr) return ;
32+
33+
int i = hash(r->key);
34+
while (table[i] != nullptr) {
35+
i = (i + 1) % MAX_SIZE;
36+
}
37+
table[i] = r;
38+
}
39+
40+
// NOTE: search
41+
pRecord search(int key) {
42+
int i = key;
43+
while (table[ hash(i) ] != nullptr && table[ hash(i) ]->key != key) {
44+
i = (i + 1) % MAX_SIZE;
45+
}
46+
return table[ hash(i) ];
47+
}
48+
49+
// NOTE: remove
50+
void remove(int key) {
51+
int i = key;
52+
while (table[ hash(i) ] != nullptr && table[ hash(i) ]->key != key) {
53+
i = (i + 1) % MAX_SIZE;
54+
}
55+
56+
table[ hash(i) ] = nullptr;
57+
58+
for (i = (i + 1) % MAX_SIZE ; table[ hash(i) ] != nullptr; i = (i + 1) % MAX_SIZE) {
59+
insert(table[ hash(i) ]);
60+
table[ hash(i) ] = nullptr;
61+
}
62+
}
63+
64+
private:
65+
static constexpr int MAX_SIZE = 97;
66+
67+
pRecord table[ MAX_SIZE ];
68+
69+
// NOTE: hash function.
70+
int hash(int key) {
71+
return (key % MAX_SIZE);
72+
}
73+
};
74+
75+
#endif // ifndef _HASH_TABLE_HPP_

0 commit comments

Comments
 (0)