-
Notifications
You must be signed in to change notification settings - Fork 101
/
HashTableSeperateChaining.cpp
89 lines (76 loc) · 1.62 KB
/
HashTableSeperateChaining.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
/*Implementaion of Hash Table using Seperate Chaining*/
#include <bits/stdc++.h>
using namespace std;
class pairs
{
public:
int key;
int value;
pairs(int k,int v) {
this->key = k;
this->value = v;
}
};
class Hash_table
{
private:
int capacity;
list<pairs*> *table;
int hashfunc(int key)
{
return key % capacity;
}
public:
Hash_table(int cap)
{
this->capacity = cap;
table = new list<pairs*>[capacity];
}
void insert(int key, int value)
{
pairs *new_pair = new pairs(key,value);
int hashindex = hashfunc(key);
table[hashindex].push_back(new_pair);
}
void deleteitem(int key)
{
int hashindex = hashfunc(key);
list <pairs*> :: iterator it;
for (it = table[hashindex].begin(); it != table[hashindex].end(); it++)
{
if ((*it)->key == key)
{
break;
}
}
if (it != table[hashindex].end())
{
table[hashindex].erase(it);
}
}
void show()
{
for (int i = 0;i < capacity; ++i)
{
cout << i << " : " ;
for (auto x : table[i])
cout << "(" << (x)->key << "," << (x)->value << ")" << "->";
cout << "NULL\n";
}
}
};
int main()
{
Hash_table gg(10);
gg.insert(10,100);
gg.insert(30,200);
gg.insert(20,300);
gg.insert(50,400);
gg.insert(70,500);
gg.insert(60,600);
gg.insert(24,900);
gg.insert(34,69);
gg.insert(86,88);
gg.deleteitem(50);
gg.show();
}