Skip to content

Commit d49027d

Browse files
committed
新增HashTable 线性探索
1 parent c5407f3 commit d49027d

2 files changed

Lines changed: 97 additions & 2 deletions

File tree

.vscode/settings.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
22
"editor.fontFamily": "\"Inziu Iosevka SC,'Fira Code', Consolas, 'Courier New', monospace\"",
33
"editor.fontLigatures": true,
4-
"files.autoSave":"afterDelay",
4+
"files.autoSave": "afterDelay",
55
"editor.fontSize": 16,
66
"files.autoSaveDelay": 1000,
7-
"editor.foldingStrategy": "indentation"
7+
"editor.foldingStrategy": "indentation",
8+
"workbench.colorCustomizations": {
9+
"editor.selectionBackground": "#666b6de5"
10+
},
11+
"editor.selectionHighlight": true,
12+
"editor.occurrencesHighlight": true
813

914
}

DictionaryFiles/HashTabLinear.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//HashTable 线性探查
2+
function HashTableLinear {
3+
//自定义节点属性
4+
let Node = function (element) {
5+
this.element = element;
6+
this.next = null;
7+
};
8+
9+
//定义散列表私有散列函数
10+
let loseloseHashCode = function (key) {
11+
let hash = 0;
12+
for (let i = 0; i < key.length; i++) {
13+
hash = hash + key.charCodeAt(i);
14+
}
15+
return hash % 37;
16+
};
17+
18+
//创建更好或者更受欢迎的散列函数
19+
let dbjHashCode = function (key) {
20+
var hash = 5318;
21+
for (var i = 0; i < key.length; i++) {
22+
hash = hash * 33 + key.charCodeAt(i);
23+
}
24+
return hash % 1013;
25+
};
26+
27+
//内部辅助类
28+
var keyValuePair = function (key, value) {
29+
this.key = key;
30+
this.value = value;
31+
this.toString = function () {
32+
return '[' + this.key + '-' + this.value + ']';
33+
}
34+
};
35+
36+
//添加元素
37+
this.put = function (key, value) {
38+
var position = loseloseHashCode(key);
39+
if (tab[position] == undefined) {
40+
//如果当前位置为undefined 则为该位置创建链表对象
41+
tab[position] = new keyValuePair(key, value);
42+
} else {
43+
var index = ++position;
44+
while (tab[index] != undefined) {
45+
index++;
46+
}
47+
//检索到index对应的位置为undefined
48+
tab[index] = new keyValuePair(key, value);
49+
}
50+
};
51+
52+
//根据键值从散列表中删除值
53+
this.remove = function (key) {
54+
let position = loseloseHashCode(key);
55+
if (tab[position] != undefined) {
56+
if (tab[position].key == key) {
57+
return tab[position].value;
58+
} else {
59+
var index = ++position;
60+
while (tab[index] === undefined || tab[index].key != key) {
61+
index++;
62+
}
63+
if (tab[index].key === key) {
64+
return tab[index].value;
65+
}
66+
}
67+
}
68+
return undefined;
69+
};
70+
71+
//返回根据键值检索的特定值
72+
this.get = function (key) {
73+
let position = loseloseHashCode(key);
74+
//判断当前位置是否存在值
75+
if (tab[position] != undefined) {
76+
if (tab[position].key == key) {
77+
return tab[position].value;
78+
} else {
79+
var index = ++position;
80+
while (tab[index] === undefined || tab[index].key != key) {
81+
index++;
82+
}
83+
if (tab[index].key === key) {
84+
return tab[index].value;
85+
}
86+
}
87+
}
88+
return undefined;
89+
};
90+
};

0 commit comments

Comments
 (0)