-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.cpp
133 lines (114 loc) · 2.22 KB
/
hash_table.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
#include <iostream>
#include <list>
class hash_table
{
private:
int capacity;
std::list<int> *table;
public:
hash_table(int);
void insert_item(int, int);
void search_item(int);
void delete_item(int);
bool check_prime(int);
int get_prime(int);
int hash_function(int);
void display_hash();
~hash_table();
};
//Constructor
hash_table::hash_table(int size)
{
this->capacity = size;
table = new std::list<int>[capacity];
}
//insert_item
void hash_table::insert_item(int key, int data)
{
int index = hash_function(key);
table[index].push_back(data);
}
//search_item
void hash_table::search_item(int key)
{
bool element_exists = false;
int index = hash_function(key);
std::list<int>::iterator it;
for (it = table[index].begin(); it != table[index].end(); it++)
{
if (*it == key)
{
element_exists = true;
std::cout << "The element => " << key << " is present in the hash table" << std::endl;
return;
}
}
std::cout << "The element => " << key << " is not present in the hash table" << std::endl;
}
//delete_item
void hash_table::delete_item(int key)
{
int index = hash_function(key);
std::list<int>::iterator it;
for (it = table[index].begin(); it != table[index].end(); it++)
{
if (*it == key)
break;
}
if (it != table[index].end())
table[index].erase(it);
}
//check_prime
bool hash_table::check_prime(int n)
{
if (n == 1 || n == 0)
return false;
for (int i = 2; i < n / 2; i++)
{
if (n % i == 0)
return false;
}
return true;
}
//get_prime
int hash_table::get_prime(int n)
{
if (n % 2 == 0)
n++;
while (!check_prime(n))
n += 2;
return n;
}
//hash_function
int hash_table::hash_function(int key)
{
return (key % capacity);
}
//display_hash
void hash_table::display_hash()
{
for (int i = 0; i < capacity; i++)
{
std::cout << "table[" << i << "]";
for (int j : table[i])
std::cout << " --> " << j;
std::cout << std::endl;
}
}
//destructor
hash_table::~hash_table()
{
delete[] table;
}
int main()
{
int key[] = {16, 12, 25, 39, 6, 122, 5, 68, 75};
int data[] = {16, 12, 25, 39, 6, 122, 5, 68, 75};
int size = sizeof(key) / sizeof(*key);
hash_table ht(10);
for (int i = 0; i < size; i++)
ht.insert_item(key[i], data[i]);
ht.search_item(5);
ht.display_hash();
return 0;
}