zoukankan      html  css  js  c++  java
  • 哈希表结构_哈希表的实现

    哈希表的实现

                1. 这里采用链地址法实现哈希表

                2. 哈希表是一个数组storage, 这个数组中的每一个index对应一个数组(链表也可以)

                3.在数组中将key和value都存进去(最好是这样)

                4. 最终实现的哈希表中的数据形式就是:

                      [[k1, v1], [k2, v2], [k3, v3]...]

                5. 定义3个属性:

                      storage:存放哈希表数据的数组,数组中的每一个元素,又是一个数组

                      count: 已存数据的个数

                      limit: 用于标记数组的长度

                6. 哈希表中的方法

                      1. put(key, value)                  向哈希表中插入和修改元素

                      2. get(key)                         根据给定的key值去获取元素

                      3. remove(key)                      从哈希表中删除元素

                      4. isEmpty()                        判断哈希表是否为空

                      5. size()                           查看哈希表中数据个数

          7. 哈希表的代码实现

         // 哈希表设计

        // 1> 将字符串转成比较大的数字:hashCode

        // 2> 将大的数字hasCode压缩到一定的数值范围之内

        // 3> 这里将字符传换成Unicode编码: A~Z: 65~90   a~z: 97~122

        function hashTable(){
            // 哈希表的属性
            this.storage = [];
            this.count = 0;
            this.limit = 8;
    
            // 封装哈希函数类
            hashTable.prototype.hashFun = function(str, size){
                // 1. 定义hashCode变量
                var hashCode = 0;
    
                // 2. 霍纳算法,计算hashCode的值
                for(var i = 0; i < str.length; i++){
                    // 这里的常数值37是常用质数值
                    hashCode = 37 * hashCode + str.charCodeAt(i);
                }
    
                // 3. 取余操作
                var index = hashCode % size;
    
                return index;
            }
    
            // 1. 向哈希表中插入数据
            hashTable.prototype.put = function(key, value){
                // 将要插入的键转换成哈希码
                var index = this.hashFun(key, this.limit);
    
                // 根据索引值,取出storage中对应的bucket
                var bucket = this.storage[index];
    
                // 判断这个bucket是否为空
                if(bucket == undefined){
                    // 为空,则创建一个数组,并将新增的数据以数组的形式存入数组
                    bucket= [[key, value]];
                    this.storage[index] = bucket;
                }else{
                    // 若不为空,判断该数据是否为重复数据
                    // 若为重复数据,则修改value值,并返回true;
                    for(var i = 0; i < bucket.length; i++){
                        if(bucket[i][0] == key){
                            bucket[i][1] = value;
                            return true;
                        }
                    }
    
                    // 若不为重复数据,则添加key和value,并返回true;
                    bucket.push([key, value]);
                    this.count += 1;
                    return true
                }
            }
    
            // 2. 在哈希表中查找数据
            hashTable.prototype.get = function(key){
                // 首先计算输入key值的哈希码
                var index = this.hashFun(key, this.limit);
    
                // 取出哈希码对应的那个bucket
                var bucket = this.storage[index];
                if(bucket == undefined){
                    return null;
                }
    
                // 依次遍历这个bucket
                for(var k = 0; k < bucket.length; k++){
                    if(bucket[k][0] == key){
                        return bucket[k][1];
                    }
                }
    
                // 若没有找到这个key值,返回null
                return null;
            }
    
            // 3. 从哈希表中删除元素
            hashTable.prototype.remove = function(key){
                // 首先计算输入key值的哈希码
                var index = this.hashFun(key, this.limit);
    
                // 根据哈希码.取出对应的bucket
                var bucket = this.storage[index];
    
                // 依次查找bucket中的元素
                for(var i =0; i < bucket.length; i++){
                    if(bucket[i][0] == key){
                        bucket.splice(i, 1);
                        this.count -=1;
                        return true;
                    }
                }
    
                return false;
            }
    
            // 4. 判断哈希表是否为空
            hashTable.prototype.isEmpet = function(){
                return count == 0;
            }
    
            // 5. 查看哈希表中的数据个数
            hashTable.prototype.size = function(){
                return this.count;
            }
    
            // 6. 统计哈希表中存储的数据的分布情况
            hashTable.prototype.sta = function(){
                var sta = [];
                for(var s = 0; s < this.storage.length; s++){
                    if(this.storage[s] == undefined){
                        sta.push(0);
                    }else{
                        sta.push(this.storage[s].length);
                    }
                }
                return sta
            }
        }
    View Code
  • 相关阅读:
    开发一个微信小程序实例教程
    java注解
    dubbo provider提供者服务打包
    Dubbo记录
    SpringBoot整合ActiveMQ 生产者---消费者
    dubbox 的各种管理和监管
    Zookeeper 3.5启动时 8080端口被占用
    springboot整合最新版dubbo以及dubbo-admin的安装
    分布式服务框架 dubbo/dubbox 入门示例
    Maven安装配置【WIN10】
  • 原文地址:https://www.cnblogs.com/carreyBlog/p/13657088.html
Copyright © 2011-2022 走看看