// 散列表
/*
* put(key,value): 向散列表添加一个新的项
* remove(key): 根据键值从散列表中移除值
* get(key): 返回根据键值检索到的特定的值
*/
function HashTable() {
var table = [];
var loseloseHashCode = function(key) { // javascript 数据结构和算法 P80 这个散列函数不是很明白干什么用的
var hash = 0;
for(var i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
this.put = function(key, value) {
var position = loseloseHashCode(key);
console.log(position + '-' + key);
table[position] = value;
}
this.get = function(key) {
return table[loseloseHashCode(key)];
}
this.remove = function(key) {
table[loseloseHashCode(key)] = undefined;
}
this.getTb = function() {
return table;
}
}
var hash = new HashTable();
hash.put('gandalf', 'gandalf@email.com');
hash.put('rose', 'rose@email.com');
hash.put('andy', 'andy@email.com');
console.log(hash.get('gandalf'));
hash.remove('gandalf');
console.log(hash.getTb());
function HashTable() {
var table = [];
var loseloseHashCode = function(key) { //
var hash = 0;
for(var i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
this.put = function(key, value) {
var position = loseloseHashCode(key);
console.log(position + '-' + key);
table[position] = value;
}
this.get = function(key) {
return table[loseloseHashCode(key)];
}
this.remove = function(key) {
table[loseloseHashCode(key)] = undefined;
}
this.getTb = function() {
return table;
}
}
var hash = new HashTable();
hash.put('gandalf', 'gandalf@email.com');
hash.put('rose', 'rose@email.com');
hash.put('andy', 'andy@email.com');
console.log(hash.get('gandalf'));
hash.remove('gandalf');
console.log(hash.getTb());
// 上面方式创建散列表,数据够多的情况下,则会发生冲突问题,就是position不是一一对应关系,而是一对多的关系
// 处理这种冲突有几种方法: 分离链接,线性探查和双散列法
// 首先是 分离链接法:
function HashTable() {
var table = [];
var loseloseHashCode = function(key) { //
var hash = 0;
for(var i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % 37;
}
var ValuePair = function(key, value) {
this.key = key;
this.value = value;
this.toString = function() {
return '[' + this.key + '-' + this.value + ']';
}
}
this.put = function(key, value) {
var position = loseloseHashCode(key);
if(table[position] == undefined) {
table[position] = new LinkedList();
}
table[position].append(new ValuePair(key, value));
}
this.get = function(key) {
var position = loseloseHashCode(key);
if(table[position] !== undefined) {
var current = table[position].getHead();
while(current.next) {
if(current.element.key === key) {
return current.element.value;
}
current = current.next;
}
if(current.element.key === key) {// 需要查找的元素在链表的第一个或者最后一项
return current.element.value;
}
}
return undefined;
}
this.remove = function(key) {
var position = loseloseHashCode(key);
if(table[position] !== undefined) {
var current = table[position].getHead();
while(current.next) {
if(current.element.key === key) {
table[position].remove(current.element);
if(table[position].isEmpty()) {
table[position] = undefined;
}
return true;
}
current = current.next;
}
// 检查是否为第一个或者最后一个元素
if(current.element.key === key) {
table[position].remove(current.element);
if(table[position].isEmpty()) {
table[position] = undefined;
}
return true;
}
}
return false;
}
this.getTb = function() {
return table;
}
}
// 二是 线性探查法: