1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > HashTable</ title >
6
+ < script type ="text/javascript ">
7
+ function LinkedList ( ) {
8
+ //Node类表示链表中操作的项
9
+ var Node = function ( element ) {
10
+ this . element = element ;
11
+ this . next = null ;
12
+ }
13
+
14
+ var length = 0 ;
15
+ var head = null ;
16
+
17
+ //链表尾部添加一项
18
+ this . append = function ( element ) {
19
+ var node = new Node ( element ) ;
20
+ var current ;
21
+ if ( head == null ) {
22
+ head = node ;
23
+ } else {
24
+ current = head ;
25
+ while ( current . next ) {
26
+ current = current . next ;
27
+ }
28
+ current . next = node ;
29
+ }
30
+ length ++ ;
31
+ } ;
32
+
33
+ //特定位置添加元素
34
+ this . insert = function ( position , element ) {
35
+ var node = new Node ( element ) ;
36
+ var current = head ;
37
+ var previous ;
38
+ var index = 0 ;
39
+
40
+ //检查越界位置
41
+ if ( position >= 0 && position <= length ) {
42
+ if ( position === 0 ) {
43
+ node . next = current ;
44
+ head = node ;
45
+ } else {
46
+ while ( index ++ < position ) {
47
+ previous = current ;
48
+ current = current . next ;
49
+ }
50
+ node . next = current ;
51
+ previous . next = node ;
52
+ }
53
+ length ++ ;
54
+ return true ;
55
+ } else {
56
+ return false ;
57
+ }
58
+ } ;
59
+
60
+ //从链表中移除特定位置一项
61
+ this . removeAt = function ( position ) {
62
+
63
+ if ( position > - 1 && position < length ) { //下标越界检查
64
+ var current = head ;
65
+ var previous ;
66
+ var index = 0 ;
67
+
68
+ if ( position === 0 ) {
69
+ head = current . next ;
70
+ } else {
71
+ while ( index ++ < position ) {
72
+ previous = current ;
73
+ current = current . next ;
74
+ }
75
+ previous . next = current . next ;
76
+ }
77
+ length -- ;
78
+ return current . element ;
79
+ } else {
80
+ return null ;
81
+ }
82
+ } ;
83
+
84
+ //从链表中移除一项
85
+ this . remove = function ( element ) {
86
+ var index = this . indexOf ( element ) ;
87
+ return this . removeAt ( index ) ;
88
+ } ;
89
+
90
+ //返回列表中项的索引
91
+ this . indexOf = function ( element ) {
92
+ var current = head ;
93
+ var index = 0 ;
94
+ while ( current ) {
95
+ if ( element == current . element ) {
96
+ return index ;
97
+ }
98
+ index ++ ;
99
+ current = current . next ;
100
+ }
101
+ return - 1 ;
102
+ } ;
103
+ //判空
104
+ this . isEmpty = function ( ) {
105
+ return length === 0 ;
106
+ } ;
107
+ //链表中的长度
108
+ this . size = function ( ) {
109
+ return length ;
110
+ } ;
111
+
112
+ //返回第一个元素
113
+ this . getHead = function ( ) {
114
+ return head ;
115
+ }
116
+
117
+ this . toString = function ( ) {
118
+ var current = head ;
119
+ var string = '' ;
120
+ while ( current ) {
121
+ string += current . element ;
122
+ current = current . next ;
123
+ }
124
+ return string ;
125
+ } ;
126
+
127
+ this . print = function ( ) {
128
+ console . log ( this . toString ( ) ) ;
129
+ }
130
+ }
131
+
132
+
133
+ function HashTable ( ) {
134
+ var table = [ ] ;
135
+
136
+ //散列函数
137
+ var loseloseHashCode = function ( key ) {
138
+ var hash = 5381 ;
139
+ for ( var i = 0 ; i < key . length ; i ++ ) {
140
+ hash = hash * 33 + key . charCodeAt ( i ) ;
141
+ }
142
+ return hash % 1013 ;
143
+ } ;
144
+
145
+ //散列表辅助类
146
+ var ValuePair = function ( key , value ) {
147
+ this . key = key ;
148
+ this . value = value ;
149
+
150
+ this . toString = function ( ) {
151
+ return '[' + this . key + '-' + this . value + ']'
152
+ }
153
+ } ;
154
+
155
+ //向散列表增加一项
156
+ this . put = function ( key , value ) {
157
+ var position = loseloseHashCode ( key ) ;
158
+ if ( table [ position ] == undefined ) {
159
+ table [ position ] = new LinkedList ( ) ;
160
+ }
161
+ table [ position ] . append ( new ValuePair ( key , value ) ) ;
162
+ } ;
163
+
164
+ //根据键值从散列表中移除一项
165
+ this . remove = function ( key ) {
166
+ var position = loseloseHashCode ( key ) ;
167
+
168
+ if ( table [ position ] !== undefined ) {
169
+ var current = table [ position ] . getHead ( ) ;
170
+ while ( current . next ) {
171
+ if ( current . element . key === key ) {
172
+ table [ position ] . remove ( current . element ) ;
173
+ if ( table [ position ] . isEmpty ( ) ) {
174
+ table [ position ] = undefined ;
175
+ }
176
+ return true ;
177
+ }
178
+ current = current . next ;
179
+ }
180
+
181
+ if ( current . element . key === key ) {
182
+ table [ position ] . remove ( current . element ) ;
183
+ if ( table [ position ] . isEmpty ( ) ) {
184
+ table [ position ] = undefined ;
185
+ }
186
+ return true ;
187
+ }
188
+ }
189
+
190
+ return false ;
191
+ } ;
192
+
193
+ //返回根据键值检索到的特定的值
194
+ this . get = function ( key ) {
195
+ var position = loseloseHashCode ( key ) ;
196
+
197
+ if ( table [ position ] !== undefined ) {
198
+ //找到起始指针
199
+ var current = table [ position ] . getHead ( ) ;
200
+
201
+ //遍历链表寻找键值对
202
+ while ( current . next ) {
203
+ if ( current . element . key === key ) {
204
+ return current . element . value ;
205
+ }
206
+ current = current . next ;
207
+ }
208
+
209
+ //检查元素在链表第一个和最后一个节点的情况
210
+ if ( current . element . key === key ) {
211
+ return current . element . value ;
212
+ }
213
+ }
214
+
215
+ return undefined ;
216
+ } ;
217
+ }
218
+ </ script >
219
+ </ head >
220
+ < body >
221
+
222
+ </ body >
223
+ </ html >
0 commit comments