การใช้งาน JavaScript ของพจนานุกรม
แนวคิดการเขียนโปรแกรม:
รหัส:
function () {"ใช้อย่างเข้มงวด"; ฟังก์ชันพจนานุกรม () {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 () {สำหรับ (var key ใน this.datastore) {ลบ this.datastore [key]; } this._size = 0; - Dictionary.prototype.add = ฟังก์ชั่น (คีย์, ค่า) {this.datastore [key] = value; this._size ++; - Dictionary.prototype.find = function (key) {return this.datastore [key]; - Dictionary.prototype.count = function () {var n = 0; สำหรับ (คีย์ var ใน this.datastore) {n ++; } return n; - Dictionary.prototype.remove = function (key) {ลบ this.datastore [key]; this._size--; - Dictionary.prototype.showall = function () {สำหรับ (var key ใน this.datastore) {console.log (คีย์ + "->" + this.datastore [key]); - module.exports = พจนานุกรม;}) ();การใช้งาน JavaScript ของ Hash (Hashtable)
แนวคิดการเขียนโปรแกรม:
รหัส:
Valuepair.js
(function () {"ใช้อย่างเข้มงวด"; ฟังก์ชั่น valuepair (คีย์, ค่า) {this.key = key; this.value = value;} valuepair.prototype.toString = function () {return "[" + this.key + ":" this.value + "]";};};};};};};};hashtable.js
(ฟังก์ชั่น () {"ใช้อย่างเข้มงวด"; var valuepair = ต้องการ ("./ lib/valuepair"); var linkedList = ต้องการ ("./ linkedList"); ฟังก์ชั่น hashtable () {this.table = object.create (null); this._size = 0; hashtable.prototype.size = function () {return._size;}; = currnode.next; } else {var currnode = this.table [index] .gethead (); if (this.table [index] == null) {this.table [index] = new LinkedList (); ค่า BREAK; สำหรับ (คีย์ var ใน this.table) {var currnode = this.table [key] .gethead (); *************************/ฟังก์ชั่น HashCode (คีย์) {// อัลกอริทึมของฮอร์เนอร์หมายเลขนายกคือ 37 var hashvalue = 6011; hashtable;}) ();