-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHashMap.html
80 lines (74 loc) · 1.97 KB
/
HashMap.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function Node(key, value) {
this.key = key
this.value = value
this.next = null
}
function HashMap() {
this.res = []
}
HashMap.prototype.add = function (key, value) {
let hashKey = key.charCodeAt(0) % 10
if (!this.res[hashKey]) {
this.res[hashKey] = node
} else {
let pNode = this.res[hashKey]
while (pNode.next) {
pNode = pNode.next
}
pNode.next = node
}
}
HashMap.prototype.find = function (key, value) {
let pNode = this.contain(key, value)
if (!pNode) {
return false
} else {
while (pNode) {
if (pNode.key === key && pNode.value === value) {
return pNode
}
pNode = pNode.next
}
return false
}
}
HashMap.prototype.remove = function (key, value) {
let pNode = this.contain(key, value)
if (!pNode) {
return false
} else {
if (key === pNode.key && value === pNode.value) {
let next = pNode.next
this.res[hashKey] = next
} else {
let pre = pNode,
node = pNode.next
while (node) {
if (node.key === key && node.value === value) {
pre.next = node.next
node = null
return true
}
node = node.next
pre = pre.next
}
return false
}
}
}
HashMap.prototype.contain = function (key, value) {
let hashKey = key.charCodeAt(0) % 10
let pNode = this.res[hashKey]
return pNode
}
</script>
</body>
</html>