哈希表 添加 增添 删除 获取方法 Js封装

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //设计哈希函数
        // 1>将字符串转化为较大的数字HashCode
        // 2>将较大数字压缩到数组
        function hashFunc(str, size) {
            // 1.定义变量
            var hashCode = 0;
            // 2.霍纳法则
            for (var i = 0; i < str.length; i++) {
                hashCode = 37 * hashCode + str.charCodeAt(i);

            }
            //取余操作
            var index = hashCode % size;
            return index

        }
        //测试
        console.log(hashFunc('abc', 7))
            //哈希表
        function HashTable() {
            //属性
            this.storage = [] //数组
            this.count = 0 //存放数的个数
            this.limit = 7 //数组长度
                //方法
            HashTable.prototype.hashFunc = function(str, size) {
                    // 1.定义变量
                    var hashCode = 0;
                    // 2.霍纳法则
                    for (var i = 0; i < str.length; i++) {
                        hashCode = 37 * hashCode + str.charCodeAt(i);

                    }
                    //取余操作
                    var index = hashCode % size;
                    return index

                }
                //插入/修改
            HashTable.prototype.put = function(key, value) {
                    //获取key对应index
                    var index = this.hashFunc(key, this.limit)
                        //判断bucket是否为空
                    var bucket = this.storage[index];
                    //判断bucket是否为龙
                    if (bucket == null) {
                        bucket = []
                        this.storage[index] = bucket
                    }
                    for (var i = 0; i < bucket.length; i++) {
                        var tuple = bucket[i]
                        if (tuple[0] == key) {
                            tuple[1] = value
                            return true
                        }
                    }
                    bucket.push([key, value])
                    this.count++

                }
                //获取
            HashTable.prototype.get = function(key) {
                //更根据key获取对应的index
                var index = this.hashFunc(key, this.limit)
                    //更具index获取对应的bucket
                var bucket = this.storage[index]
                    //判断bucket是否为空
                if (bucket == null) {
                    return null
                }
                for (var i = 0; i < bucket.length; i++) {
                    var tuple = bucket[i]
                    if (tuple[0] = bucket[i]) {
                        return tuple[1]
                    }
                }
            }
            HashTable.prototype.remove = function(key) {
                //更根据key获取对应的index
                var index = this.hashFunc(key, this.limit)
                    //更具index获取对应的bucket
                var bucket = this.storage[index]
                    //判断bucket是否为空
                if (bucket == null) {
                    return null
                }
                for (var i = 0; i < bucket.length; i++) {
                    var tuple = bucket[i]
                    if (tuple[0] = bucket[i]) {
                        bucket.splice(i, 1)
                        this.count--
                            return tuple[1]
                    }
                }
                return null
            }
            HashTable.prototype.isEmpty = function() {
                return this.count == 0
            }
            HashTable.prototype.size = function() {
                return this.count
            }
        }
        //测试
        var ht = new HashTable()
            //插入
        ht.put('acs', '123')
        ht.put('cba', '321')
        ht.put('nba', '521')
        ht.put('mba', '520')
        console.log(ht)
            //据哦去
        ht.put("acs", '111')
        console.log(ht.get('acs'))
            //删除
        ht.remove("acs")
        console.log(ht.get('acs'))
    </script>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优价实习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值