字典(Dictionary)的javascript实现
编程思路:
- 使用了裸对象datastore来进行元素存储;
- 实现了两种得到字典长度的方法,一种为变量跟踪,一种为实时计算。
代码:
function(){ "use strict"; function Dictionary(){ this._size = 0; this.datastore = Object.create(null); } Dictionary.prototype.isEmpty = function(){ return this._size === 0; }; Dictionary.prototype.size = function(){ return this._size; }; Dictionary.prototype.clear = function(){ for(var key in this.datastore){ delete this.datastore[key]; } this._size = 0; }; Dictionary.prototype.add = function(key, value){ this.datastore[key] = value; this._size++; }; Dictionary.prototype.find = function(key){ return this.datastore[key]; }; Dictionary.prototype.count = function(){ var n = 0; for(var key in this.datastore){ n++; } return n; }; Dictionary.prototype.remove = function(key){ delete this.datastore[key]; this._size--; }; Dictionary.prototype.showAll = function(){ for(var key in this.datastore){ console.log(key + "->" + this.datastore[key]); } }; module.exports = Dictionary; })();
散列(hashtable)的javascript实现
编程思路:
- 以链表来解决实现开链法来解决碰撞,并使用自己写的单链表库LinkedList(详见jb51之前的https://www.jb51.net/article/86394.htm);
- 用裸对象来存储;
- ValuePair简单封装键值对;
- 以模块模式组织代码;
代码:
valuePair.js
(function(){ "use strict"; function ValuePair(key, value){ this.key = key; this.value = value; } ValuePair.prototype.toString = function(){ return "[" + this.key + ":" + this.value + "]"; }; module.exports = ValuePair; })();
hashtable.js
(function(){ "use strict"; var ValuePair = require("./lib/ValuePair"); var LinkedList = require("./LinkedList"); function Hashtable(){ this.table = Object.create(null); this._size = 0; } Hashtable.prototype.isEmpty = function(){ return this._size === 0; }; Hashtable.prototype.size = function(){ return this._size; }; Hashtable.prototype.remove = function(key){ var index = hashCode(key); if(this.table[index] == null){ return false; }else{ var currNode = this.table[index].getHead(); while(currNode.next){ currNode = currNode.next; if(currNode.element.key == key){ this.table[index].remove(currNode.element); this._size--; return true; } } return false; } }; Hashtable.prototype.get = function(key){ var index = hashCode(key); if(this.table[index] == null){ return null; }else{ var currNode = this.table[index].getHead(); while(currNode.next){ currNode = currNode.next; if(currNode.element.key == key){ return currNode.element; } } return null; } }; Hashtable.prototype.put = function(key, value){ var index = hashCode(key); if(this.table[index] == null){ this.table[index] = new LinkedList(); } var currNode = this.table[index].getHead(); while(currNode.next){ //key若已经存在,修改value值为新值 currNode = currNode.next; if(currNode.element.key == key){ currNode.element.value = value; break; } } if(currNode.next == null && currNode.element.value != value){ //key不存在,加入新值.注意边界值 this.table[index].add(new ValuePair(key,value)); this._size++; } return this; }; Hashtable.prototype.display = function(){ for(var key in this.table){ var currNode = this.table[key].getHead(); while(currNode.next){ currNode = currNode.next; console.log(currNode.element.toString()); } } }; /*********************** Utility Functions ********************************/ function hashCode(key) { //霍纳算法,质数取37 var hashValue = 6011; for (var i = 0; i < key.length; i++) { hashValue = hashValue * 37 + key.charCodeAt(i); } return hashValue % 1019; } module.exports = Hashtable; })();
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】
- 群星.2021-一生一世电视剧原声带【韶愔音乐】【FLAC分轨】
- [试音碟]iFiQuadDSDSamplerAlbum[24-44,1]WAV
- 姚璎格《发烧女中音音响人声示范碟[紫银合金SQCD]》[正版CD低速原抓WAV+CUE]
- 群星《烈火青春》1988[WAV+CUE]
- 蓝心湄.2000-你开心了湄2CD【SONY】【WAV+CUE】
- 谭咏麟.1989-像我这样的朋友【宝丽金】【WAV+CUE】
- 罗文.2022-大地留着的记号·纪念罗文辞别廿载3CD【环球】【WAV+CUE】
- 670《脆弱敏感小女生》[FLAC/分轨][78.47MB]
- 时代少年团《「叁重楼」演唱会 -「楼间楼」重庆站LIVE》[320K/MP3][278.08MB]