-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
97 lines (75 loc) · 1.29 KB
/
HashTable.cpp
File metadata and controls
97 lines (75 loc) · 1.29 KB
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
#include<iostream>
using namespace std;
struct Wezel{
int data = 0;
Wezel* next = nullptr;
};
struct HashTable {
static const int size = 20;
Wezel* table[size];
HashTable()
{
for (int i = 0; i < size; i++)
{
*(table + i) = nullptr;
}
}
int hashfuntion(int key) const {
return key % size;
}
void insert(int value)
{
int hashKey = hashfuntion(value);
Wezel* w = table[hashKey];
Wezel* nowy = new Wezel{ value };
if (w == nullptr)
{
table[hashKey] = nowy;
return;
}
while (w->next != nullptr)
{
w = w->next;
}
w->next = nowy;
}
int chainLength(int value) const {
int hashKey = hashfuntion(value);
Wezel* w = table[hashKey];
if (w == nullptr) return 0;
int size = 1;
while (w->next != nullptr)
{
size++;
w = w->next;
}
return size;
}
void longestChain()
{
int longestChainSize = 0;
for (int i = 0; i < size; i++)
{
int length = chainLength(i);
cout << "LENGTH: " << length << '[' << i << ']' << '\n';
if (length > longestChainSize)
{
longestChainSize = length;
}
}
cout << "LONGEST CHAIN SIZE: " << longestChainSize << '\n';
}
};
int main()
{
HashTable HT;
const int n = 10;
int value = 0;
for (int i = 0; i < n; i++)
{
cin >> value;
HT.insert(value);
}
HT.longestChain();
return 0;
}