JavaScript中實現索引值對應的字典與雜湊表結構的樣本_javascript技巧

來源:互聯網
上載者:User

字典(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之前的http://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;})();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.