哈希表
数据结构 - 哈希表。
// 哈希表实现
// ES5
var HashTable = function() {
var items = [];
// 散列函数
// key > number > items[number]
var loseloseHashCode = function(key) {
var hash = 0;
for (var i in key) {
hash += key[i].charCodeAt();
}
return hash % 37;
};
// 添加
this.put = function(key, value) {
var position = loseloseHashCode(key);
console.log(`${position} - ${value}`);
items[position] = value;
};
// 移除
this.remove = function(key) {
items[loseloseHashCode(key)] = undefined;
};
// 获取数据
this.get = function(key) {
return items[loseloseHashCode(key)];
};
// 检查items
this.getItem = function() {
return items;
};
};
var ht = new HashTable();
ht.put('Brady', 'Brady@qq.com');
ht.put('Sandy', 'Sandy@qq.com');