在哈希表的Javascript实现中调用链接列表上的方法

时间:2015-05-25 22:59:53

标签: javascript methods linked-list hashtable

我正在努力在Javascript中创建一个哈希表,以便更好地理解数据结构。我在每个表位置使用链接列表实现它以处理冲突。

问题是我无法在this.storage [index]上调用.add,因为它未定义。当我记录this.storage [index]时,我会在链表中看到我的头节点以及我需要的方法。所以我不确定问题是什么。

var Node = function(key, value) {
  this.key = key;
  this.value = value;
  this.next = null;
};

var LinkedList = function() {
  this.head = new Node('head');
  this.add = function(key, value) {
    var node = new Node(key, value);
    var curNode = this.head;
    while (curNode.next !== null) {
      curNode = curNode.next;
    }
    curNode.next = node;
  };
  this.find = function(key) {
    var curNode = this.head;
    while (curNode.key !== key && curNode.next !== null) {
      curNode = curNode.next;
    }
    return curNode.value;
  };
};

var HashTable = function(max) {
  this.max = max;
  this.storage = [];
  for (var i = 0; i < max; ++i) {
    this.storage[i] = new LinkedList();
  }
  this.hash = function(key) {
    var sum = 0;
    for (var i = 0; i < key.length; ++i) {
      sum += key[i].charCodeAt() - 97;
    }
    return sum % this.max;
  };
  this.addValue = function(key, value) {
    var index = this.hash(key);
    this.storage[index].add(key, value);
  };
  this.getValue = function(key) {
    var index = this.hash(key);
    return this.storage[index].find(key);
  };
};

var hash = new HashTable(5);
hash.addValue("I");
hash.addValue("would");
hash.addValue("like");
hash.addValue("coffee");
hash.getValue('I');

1 个答案:

答案 0 :(得分:0)

Javascript模数运算符并不像您对负数那样工作。如果sum-4,则sum % max不是max - 4,而是-4。然后尝试添加到this.storage[-4],这是未定义的。您需要检查并调整。

&#13;
&#13;
var Node = function(key, value) {
  this.key = key;
  this.value = value;
  this.next = null;
};

var LinkedList = function() {
  this.head = new Node('head');
  this.add = function(key, value) {
    var node = new Node(key, value);
    var curNode = this.head;
    while (curNode.next !== null) {
      curNode = curNode.next;
    }
    curNode.next = node;
  };
  this.find = function(key) {
    var curNode = this.head;
    while (curNode.key !== key && curNode.next !== null) {
      curNode = curNode.next;
    }
    return curNode.value;
  };
};

var HashTable = function(max) {
  this.max = max;
  this.storage = [];
  for (var i = 0; i < max; ++i) {
    this.storage[i] = new LinkedList();
  }
  this.hash = function(key) {
    var sum = 0;
    for (var i = 0; i < key.length; ++i) {
      sum += key[i].charCodeAt() - 97;
    }
    sum = sum % this.max;
    if (sum < 0) {
        sum = this.max + sum;
    }
    return sum;
  };
  this.addValue = function(key, value) {
    var index = this.hash(key);
    this.storage[index].add(key, value);
  };
  this.getValue = function(key) {
    var index = this.hash(key);
    return this.storage[index].find(key);
  };
};

var hash = new HashTable(5);
hash.addValue("I");
hash.addValue("would");
hash.addValue("like");
hash.addValue("coffee");
hash.getValue('I');
console.log(hash);
&#13;
&#13;
&#13;