JavaScript中實(shí)現(xiàn)鍵值對(duì)應(yīng)的字典與哈希表結(jié)構(gòu)的示例
字典(Dictionary)的javascript實(shí)現(xiàn)
編程思路:
- 使用了裸對(duì)象datastore來(lái)進(jìn)行元素存儲(chǔ);
- 實(shí)現(xiàn)了兩種得到字典長(zhǎng)度的方法,一種為變量跟蹤,一種為實(shí)時(shí)計(jì)算。
代碼:
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實(shí)現(xiàn)
編程思路:
- 以鏈表來(lái)解決實(shí)現(xiàn)開鏈法來(lái)解決碰撞,并使用自己寫的單鏈表庫(kù)LinkedList(詳見jb51之前的http://www.dbjr.com.cn/article/86394.htm);
- 用裸對(duì)象來(lái)存儲(chǔ);
- ValuePair簡(jiǎn)單封裝鍵值對(duì);
- 以模塊模式組織代碼;
代碼:
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若已經(jīng)存在,修改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) { //霍納算法,質(zhì)數(shù)取37 var hashValue = 6011; for (var i = 0; i < key.length; i++) { hashValue = hashValue * 37 + key.charCodeAt(i); } return hashValue % 1019; } module.exports = Hashtable; })();
相關(guān)文章
JS高仿拋物線加入購(gòu)物車特效實(shí)現(xiàn)代碼
本篇文章主要介紹了JS高仿拋物線加入購(gòu)物車特效實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02jser必看的破解javascript各種加密的反向思維方法
才發(fā)現(xiàn)的破解javascript各種加密的反向思維方法,大家有好的方法都跟帖啊最近發(fā)現(xiàn)了一個(gè)代碼,加密了5層左右,我將破解到最后一步,而且不用javascript解密程序2007-04-04javascript數(shù)組使用調(diào)用方法匯總
javascript數(shù)組使用調(diào)用方法匯總...2007-12-12JavaScript使用push方法添加一個(gè)元素到數(shù)組末尾用法實(shí)例
這篇文章主要介紹了JavaScript使用push方法添加一個(gè)元素到數(shù)組末尾,實(shí)例分析了javascript中push函數(shù)的使用技巧,需要的朋友可以參考下2015-04-04javascript的函數(shù)、創(chuàng)建對(duì)象、封裝、屬性和方法、繼承
從一開始接觸到j(luò)s就感覺好靈活,每個(gè)人的寫法都不一樣,比如一個(gè)function就有N種寫法2011-03-03用Javascript同時(shí)提交多個(gè)Web表單的方法
使用Javascript同時(shí)提交多個(gè)Web表單的方法2009-12-12