-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_probing.js
More file actions
61 lines (46 loc) · 1.13 KB
/
linear_probing.js
File metadata and controls
61 lines (46 loc) · 1.13 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
// Linear Porbing
class HashTable {
constructor(size = 100) {
this.size = new Array(size);
this.values = {};
this.length = 0;
}
hash(key){
return key.toString().length % this.size.length;
}
add(key,value){
let index = this.hash(key);
if(!this.values.hasOwnProperty(index))
this.values[index] = [];
if(!this.values[index].hasOwnProperty(key))
this.length++;
this.values[index][key] = value;
}
search(key){
let index = this.hash(key);
if(this.values.hasOwnProperty(index) && this.values[index].hasOwnProperty(key))
return this.values[index][key];
return null;
}
remove(key) {
let item = this.hash(key);
return this.values[item] = undefined;
}
size(){
return this.length;
}
isEmpty() {
for( let i=0, len = this.length; i < len; i++) {
if( this.values[i] ) {
return false
};
}
return true
}
}
const ht = new HashTable();
ht.add("Canada","300");
ht.add("Germany","100");
ht.add("Italy","50");
ht.add("Ahmoo","150");
console.log(ht);