zoukankan      html  css  js  c++  java
  • 散列碰撞问题的解决——开链法(拉链法)

        function HashTable() {
            this.table = new Array(137);//137——官方比较好的设置数组大小的值
            this.buildChains = buildChains;
            this.simpleHash = simpleHash;
            this.showDistro = showDistro;
            this.put = put;
            //this.get=get;
        }
        function buildChains() {//核心方法
            for ( var i = 0; i < this.table.length; ++i) {
                this.table[i] = new Array();
            }
        }
        function simpleHash(data) {
            var total = 0;
            for ( var i = 0; i < data.length; ++i) {
                total += data.charCodeAt(i);
            }
            return total % this.table.length;
        }
        function put(data) {
            var pos = this.simpleHash(data);
            var index = 0;
            if (this.table[pos][index] == undefined) {
                this.table[pos][index] = data;
            } else {
                while (this.table[pos][index] != undefined) {
                    ++index;
                }
                this.table[pos][index] = data;
            }
        }
        function showDistro() {
            for ( var i = 0; i < this.table.length; ++i) {
                if (this.table[i][0] != undefined) {
                    document.write(i + ": " + this.table[i]);
                    document.write("<br />");
                }
            }
        }
        var hTable = new HashTable();
        hTable.buildChains();
        var someNames = [ "David", "Jennifer", "Donnie", "Raymond", "Cynthia",
                "Mike", "Clayton", "Danny", "Jonathan" ];
        for ( var i = 0; i < someNames.length; ++i) {
            hTable.put(someNames[i]);
        }
        hTable.showDistro();
        /*上述程序运行结果为:
        35: Cynthia
        45: Raymond,Clayton
        57: Donnie
        77: David
        95: Danny
        116: Mike
        132: Jennifer
        134: Jonathan*/
  • 相关阅读:
    如何使用API创建OpenStack虚拟机?
    Windows Server 2012 新特性:IPAM的配置
    DNSSec
    Win Server 8中的利器:微软在线备份服务
    AD RMS总结
    开发中辅助功能
    开发中坑爹的地方
    Js 中常用方法
    asp.net 错误处理
    js中的注意事项(持续整理)
  • 原文地址:https://www.cnblogs.com/feile/p/5396773.html
Copyright © 2011-2022 走看看