JavaScript数据结构-散列表

HashTable类,是Dictionary类的一种散列表的实现方式。
实现如下

function HashTable() {
  var table = [];
  //散列函数
  //charCodeAt()返回指定位置的字符Unicode编码,0-65535之间的整数
  //为了得到较小的数,最后与任意数做除法得到余数
  var getHashCode = function (key) {
    var hash = 0;
    for(i = 0; i < key.length; i++){
      hash += key.charCodeAt(i);
    }
    return hash%37;
  }
  //向散列表增加一个新的项
  this.put = function (key, value) {
    var position = getHashCode(key);
    console.log(position + '-' + key);
    table[position] = value;
  }
  //根据键值检索特定的值
  this.get = function (key) {
    return table[getHashCode(key)];
  }
  //根据键值从散列表中移除
  this.remove = function (key) {
    table[getHashCode(key)] = undefined;
  }
}

var hash = new HashTable();
hash.put('bob','123')
hash.put('jack','345')
hash.put('black','567')
hash.put('sandy','000')

console.log(hash.get('bob'))
console.log(hash.get('boby'))
hash.remove('bob')
console.log(hash.get('bob'))

散列集合
散列集合只储存唯一的不重复的值

分离连接

function HashTable() {
  var table = [];
  //散列函数
  //charCodeAt()返回指定位置的字符Unicode编码,0-65535之间的整数
  //为了得到较小的数,最后与任意数做除法得到余数
  var getHashCode = function (key) {
    var hash = 0;
    for (i = 0; i < key.length; i++) {
      hash += key.charCodeAt(i);
    }
    return hash % 37;
  }
  //向散列表增加一个新的项
  this.put = function (key, value) {
    var position = getHashCode(key);
    if (table[position] == undefined) {
      table[position] = new LinkedList();
    }
    table[position].append(new ValuePair(key, value));
  }
  //根据键值检索特定的值
  this.get = function (key) {
    var position = getHashCode(key);
    if (table[position] !== undefined) {
      var current = table[position].getHeader();
      while (current.next) {
        if (current.element.key == key) {
          return current.element.value;
        }
        current = current.next;
      }
      if (current.element.key === key) {
        return current.element.value;
      }
    }
    return undefined;
  }
  //根据键值从散列表中移除
  this.remove = function (key) {
    var position = getHashCode(key);
    if (table[position] !== undefined) {
      var current = table[position].getHeader();
      while (current.next) {
        if (current.element.key === key) {
          table[position].remove(current.element);
          if (table[position].isEmpty()) {
            table[position] = undefined;
          }
          return true;
        }
        current = current.next;
      }
      if (current.element.key === key) {
        table[position].remove(current.element);
        if (table[position].isEmpty()) {
          table[position] = undefined;
        }
        return true;
      }
    }
    return false;
  }
  this.print = function () {
    for(var i = 0; i < table.length; i++){
      console.log(i + ":" + table[i]);
    }
  }
}

var ValuePair = function (key, value) {
  this.key = key;
  this.value = value;
  this.toString = function () {
    return '[' + this.key + '-' + this.value + ']';
  }
}

线性探查

var ValuePair = function (key, value) {
  this.key = key;
  this.value = value;
  this.toString = function () {
    return '[' + this.key + '-' + this.value + ']';
  }
}
function HashTable() {
  var table = [];
  //散列函数
  //charCodeAt()返回指定位置的字符Unicode编码,0-65535之间的整数
  //为了得到较小的数,最后与任意数做除法得到余数
  var getHashCode = function (key) {
    var hash = 0;
    for(i = 0; i < key.length; i++){
      hash += key.charCodeAt(i);
    }
    return hash%37;
  }
  //向散列表增加一个新的项
  this.put = function (key, value) {
    var position = getHashCode(key);
    if(table[position]==undefined){
      table[position] = new ValuePair(key, value);
    }else{
      var index = ++position;
      while (table[index] != undefined) {
        index++;
      }
      table[index] = new ValuePair(key, value)
    }
  }
  //根据键值检索特定的值
  this.get = function (key) {
    var position = getHashCode(key);
    if(table[position] !== undefined){
      if (table[position].key === key) {
        return table[position].value;
      } else {
        var index = ++position;
        while (table[index] === undefined || table[position].key !== key) {
          index++;
        }
        if (table[position].key === key) {
          return table[position].value;
        }
      }
    }
    return undefined;
  }
  //根据键值从散列表中移除
  this.remove = function (key) {
    var position = getHashCode(key);
    if(table[position] !== undefined){
      if (table[position].key === key) {
        return table[position] = undefined;
      } else {
        var index = ++position;
        while (table[index] === undefined || table[index].key !== key) {
          index++;
        }
        if (table[index].key === key) {
          return table[index] = undefined;
        }
      }
    }
    return undefined;
  }
  this.print = function () {
    for(var i = 0; i < table.length; i++){
      console.log(i + ":" + table[i]);
    }
  }
}

有兴趣可以加入Nodejs交流群,和大佬们一起成长!!!

群号:348108867

图片.png

你可能感兴趣的:(JavaScript数据结构-散列表)