forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.html
More file actions
61 lines (59 loc) · 1.54 KB
/
HashTable.html
File metadata and controls
61 lines (59 loc) · 1.54 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
/*
最基本的散列表
*/
class HashTable {
constructor() {
this.table=[];
}
//散列函数
loseHashCode(key){
var hash=0;
//从ASCII表中查到的ASCII值加到hash中
for (var i=0;i<key.length;i++){
hash+=key.charCodeAt(i);
}
//为了得到比较小的数值,我们会用hash和任意数除余
return hash%37;
}
//向散列表增加一个新的项
put(key,value){
var position=this.loseHashCode(key);
console.log(position+'-'+key);
this.table[position]=value;
}
//根据键从散列表删除值
remove(key){
this.table[this.loseHashCode(key)]=undefined;
}
//返回根据键值检索到的特定的值
get(key){
console.log(this.table[this.loseHashCode(key)])
}
print(){
for (var i=0;i<this.table.length;++i){
if (this.table[i]!==undefined){
console.log(i+':'+this.table[i]);
}
}
}
}
var hash = new HashTable();
hash.put('Gandalf', 'gandalf@email.com');
hash.put('John', 'johnsnow@email.com');
hash.put('Tyrion', 'tyrion@email.com');
hash.remove('Gandalf')
hash.get('Gandalf')
hash.get('Tyrion')
hash.print()
//
</script>
</body>
</html>