hash散列中需要确定key和value的唯一确定关系。

hash散列便于快速的插入删除和修改,不便于查找最大值等其他操作

以下为字符和数字的hash散列:

function HashTable () {
this.table = new Array(137);
this.value = new Array();
this.simpleHash = simpleHash;
this.betterHash = betterHash;
this.display = display;
this.put = put;
this.get = get;
this.buildChains = buildChains; // 开链法解决碰撞
} function simpleHash (data) {
var total = 0;
for(var i =0; i<data.length; i++){
total= total + data.charCodeAt(i);
}
return total % this.table.length;
} function betterHash (data) {
var total = 0;
const h = 37; // 挑选合适的质数
data = data.toString();
for(var i=0;i<data.length; i++){
total = total*h + data.charCodeAt(i);
}
total = total % this.table.length;
if(total<0) {
return total+=this.table.length-1;
}
return parseInt(total);
} function put (key, value) {
var pos = this.betterHash(key);
if(this.table[pos] === undefined){
this.table[pos] = key;
this.value[pos] = value;
}else{
while(this.table[pos]!==undefined){
pos++
}
this.table[pos] = key;
this.value[pos] = value;
}
} function get(key) {
var hash = -1;
hash = this.betterHash(key);
if (hash > -1) {
for (var i = hash; this.table[hash] != undefined; i++) {
if (this.table[hash] == key) {
return this.value[hash];
}
}
}
return undefined;
} function display () {
var _this = this;
this.value.forEach(function(item, index){
if (item!==undefined) {
console.log(_this.table[index] + ": " + item);
} })
} function buildChains () {
this.table.forEach(function (item, index) {
item = new Array();
})
}

hash的使用方法:

function buildChains () {
this.table.forEach(function (item, index) {
item = new Array();
})
} var someNames = ["David", "Jennifer", "Donnie", "Raymond","Cynthia", "Mike", "Clayton", "Danny", "Jonathan", "Donnie"]; var hs = new HashTable();
someNames.forEach(function(item, index){
hs.put(item, item + "Val")
})
hs.display(); console.log(hs.get("David"))

js数据结构之hash散列的详细实现方法的更多相关文章

  1. js数据结构之栈和队列的详细实现方法

    队列 队列中我们主要实现两种: 1. 常规队列 2. 优先队列(实际应用中的排队加急情况等) 常规队列的实现方法如下: // 常规队列 function Queue () { this.queue = ...

  2. javascript数据结构与算法--散列

    一:javascript数据结构与算法--散列  一:什么是哈希表? 哈希表也叫散列表,是根据关键码值(key,value)而直接进行访问的数据结构,它是通过键码值映射到表中一个位置来访问记录的,散列 ...

  3. 算法与数据结构(十二) 散列(哈希)表的创建与查找(Swift版)

    散列表又称为哈希表(Hash Table), 是为了方便查找而生的数据结构.关于散列的表的解释,我想引用维基百科上的解释,如下所示: 散列表(Hash table,也叫哈希表),是根据键(Key)而直 ...

  4. JavaScript数据结构与算法-散列练习

    散列的实现 // 散列类 - 线性探测法 function HashTable () { this.table = new Array(137); this.values = []; this.sim ...

  5. PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  6. java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区

    java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测).链地址法.再哈希.建立公共溢出区 标签: hashmaphashmap冲突解决冲突的方法冲突 2016-0 ...

  7. 【hash】什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法【关于hash的详解】

    什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法

  8. [转]c# 对密码执行散列和 salt 运算方法

    本文转自:http://www.cnblogs.com/CnBlogFounder/archive/2008/07/04/1235690.html 大家对密码执行散列和Salt运算一定不陌生.两个Vi ...

  9. 【数据结构】之散列链表(Java语言描述)

    散列链表,在JDK中的API实现是 HashMap 类. 为什么HashMap被称为“散列链表”?这与HashMap的内部存储结构有关.下面将根据源码进行分析. 首先要说的是,HashMap中维护着的 ...

随机推荐

  1. List Control控件

    List Control控件 显示方式 属性[View]选择成[Report]. 添加成员变量 绑定变量:m_listCtrl 设置值 // 表头添加 m_listCtrl.SetExtendedSt ...

  2. MR运动静止用户区分

    1.客户端打开菜单[MR]-[MR室内室外判定设置] 设置主小区是室外站且主小区信号比较强时RSRP门限 2.设置"上报数据用户临小区切换次数门限设置"值为15 mysql中t_m ...

  3. 内核中container_of宏的详细分析【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...

  4. python计算最大公约数和最小公倍数

    a=4 b=2 def gcd(a,b): return a if b==0 else gcd(b,a%b) def lcm(a,b): return a*b//gcd(a,b) print(gcd( ...

  5. python通过操作windows系统注册表方式修改环境变量

    #coding=utf8 import os import sys from subprocess import check_call if sys.hexversion > 0x0300000 ...

  6. 转载:2.2.3 配置项的注释《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19628.html 如果有一个配置项暂时需要注释掉,那么可以加"#"注释掉这一行配置.例如: #pid       ...

  7. Python-JS (JS介绍~JS的基础数据类型)

    目录一.JS语言介绍: 1.JS概念 2.JS组成 二.JS的三种存在位置(引入方式): 1.行间式: 2.内联式: 3.外联式: 三.JS出现的具体位置: 四.JS语法规范 五.JS中变量的定义 E ...

  8. C#面向对象(继承的重载和构造函数)

    构造函数: 继承的重载:

  9. LeetCode(36): 有效的数独

    Medium! 题目描述: 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1 ...

  10. python接口自动化测试四:代码发送HTTPS请求

    HTTPS: get: url = 'https://www.juhe.cn/docs/api/id/39' r = requests.get(url)                       # ...