国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript中實現(xiàn)鍵值對應(yīng)的字典與哈希表結(jié)構(gòu)的示例

2019-11-20 09:43:34
字體:
供稿:網(wǎng)友

字典(Dictionary)的javascript實現(xiàn)
編程思路:

  • 使用了裸對象datastore來進行元素存儲;
  • 實現(xiàn)了兩種得到字典長度的方法,一種為變量跟蹤,一種為實時計算。

代碼:

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實現(xiàn)
編程思路:

  • 以鏈表來解決實現(xiàn)開鏈法來解決碰撞,并使用自己寫的單鏈表庫LinkedList(詳見jb51之前的//www.survivalescaperooms.com/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若已經(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;})();

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 奉贤区| 科技| 铜鼓县| 名山县| 锡林浩特市| 康定县| 延边| 独山县| 元朗区| 鹿邑县| 全州县| 灵宝市| 泊头市| 巩义市| 加查县| 苏尼特左旗| 渭南市| 昌图县| 盐津县| 临夏县| 焉耆| 河源市| 白水县| 新泰市| 海伦市| 武安市| 边坝县| 福鼎市| 军事| 巩义市| 拉萨市| 浠水县| 宁南县| 安新县| 靖宇县| 潢川县| 永川市| 阿坝县| 沭阳县| 海盐县| 涞水县|