javascript 由plastikaweb创建的hashTable - https://repl.it/@plastikaweb/hashTable
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 由plastikaweb创建的hashTable - https://repl.it/@plastikaweb/hashTable相关的知识,希望对你有一定的参考价值。
function HashTable(size) {
this.buckets = Array(size);
this.numBuckets = this.buckets.length;
}
function HashNode(key, value, next) {
this.key = key;
this.value = value;
this.next = next || null;
}
HashTable.prototype.hash = function(key) {
var total = 0;
for (var i = 0; i < key.length; i++) {
total += key.charCodeAt(i);
}
var bucket = total % this.numBuckets;
return bucket;
};
HashTable.prototype.insert = function(key, value) {
var index = this.hash(key);
if (!this.buckets[index]) {
this.buckets[index] = new HashNode(key, value);
} else if (this.buckets[index].key === key) {
this.buckets[index].value = value;
} else {
var currentNode = this.buckets[index];
while (currentNode.next) {
if (currentNode.next.key === key) {
currentNode.next.value = value;
return;
}
currentNode = currentNode.next;
}
currentNode.next = new HashNode(key, value);
}
};
HashTable.prototype.get = function(key) {
var index = this.hash(key);
if (!this.buckets[index]) return null;
else {
var currentNode = this.buckets[index];
while (currentNode) {
if (currentNode.key === key) {
return currentNode.value;
}
currentNode = currentNode.next;
}
return null;
}
};
HashTable.prototype.retrieveAll = function() {
var allNodes = [];
for (var i = 0; i< this.numBuckets; i++) {
var currentNode = this.buckets[i];
while (currentNode) {
allNodes.push(currentNode);
currentNode = currentNode.next;
}
}
return allNodes;
};
var ht = new HashTable(30);
ht.insert('Rebecca', 'rebecca@gmail.com');
ht.insert('Dean', 'dean@gmail.com');
ht.insert('Bob', 'bob@gmail.com');
ht.insert('Dean', 'dean2@gmail.com');
console.log(ht.retrieveAll());
以上是关于javascript 由plastikaweb创建的hashTable - https://repl.it/@plastikaweb/hashTable的主要内容,如果未能解决你的问题,请参考以下文章
javascript 由plastikaweb创建的hashTable - https://repl.it/@plastikaweb/hashTable
javascript plastikaweb链接列表数据结构 - https://repl.it/@plastikaweb/LightblueTomatoRaven
javascript 数据结构链表 - addToHead()addToTail() - https://repl.it/@plastikaweb/LightblueTomatoRaven
仅使用javascript检查由javascript创建的元素是不是存在[重复]
使用 selenium webdriver 定位由 javascript 创建的元素
由javascript创建的css动画[关闭]