Javascript Data Structure Hash Table with Linear Probing
function HashTable(size) { this.size = size;/*from w w w. j ava 2 s . c om*/ this.keys = this.initArray(size); this.values = this.initArray(size); this.limit = 0; } HashTable.prototype.put = function(key, value) { if (this.limit >= this.size) throw 'hash table is full' var hashedIndex = this.hash(key); // Linear probing while (this.keys[hashedIndex] != null) { hashedIndex++; hashedIndex = hashedIndex % this.size; } this.keys[hashedIndex] = key; this.values[hashedIndex] = value; this.limit++; } HashTable.prototype.get = function(key) { var hashedIndex = this.hash(key); while (this.keys[hashedIndex] != key) { hashedIndex++; hashedIndex = hashedIndex % this.size; } return this.values[hashedIndex]; } HashTable.prototype.hash = function(key) { // Check if int if (!Number.isInteger(key)) throw 'must be int'; return key % this.size; } HashTable.prototype.initArray = function(size) { var array = []; for (var i = 0; i < size; i++) { array.push(null); } return array; } var exampletable = new HashTable(13); exampletable.put(7, "A"); console.log(exampletable.get(7)); exampletable.put(20, "B"); console.log(exampletable.get(20));