-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.h
53 lines (48 loc) · 1 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
#pragma once
#include <iostream>
#include <vector>
#include "Container.h"
#define BUCKET_SIZE 10
// HashNode stored in buckets
struct HashNode
{
int key;
int val;
HashNode *next;
HashNode(int _key, int _val)
: key(_key), val(_val), next(nullptr) {}
HashNode(int _key, int _val, HashNode *_next)
: key(_key), val(_val), next(_next) {}
};
class HashTable : public Container
{
private:
std::vector<HashNode *> bucket;
int hash(int key);
public:
HashTable()
{
for (int i = 0; i < BUCKET_SIZE; ++i)
{
bucket.push_back(nullptr);
}
}
void Insert(int key, int value);
void Search(int key);
void Delete(int key);
void Display();
~HashTable()
{
for (int i = 0; i < BUCKET_SIZE; ++i)
{
HashNode *n = bucket[i];
HashNode *n1;
while (n)
{
n1 = n->next;
delete n;
n = n1;
}
}
}
};