数据结构109-哈希表删除操作代码

简介: 数据结构109-哈希表删除操作代码
<!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>哈希表的封装</title>
</head>
<body>
    <script>
        function hashFunc(str, size) {
        var hashCode = 0;
        //霍纳算法
        for (var i = 0; i < str.length; i++) {
          hashCode = 37 * hashCode + str.charCodeAt(i);
        }
        var index=hashCode%size
        return index
      }
        function HashTable(){
            this.storage=[]
            this.count=0
            this.limit=7*2
            //方法 
            HashTable.prototype.put=function(key,value){
                //根据key获取对应的index
                var index=this.hashFunc(key,this.limit)
                //根据index取出对应的bucket
                var bucket=this.storage[index]
                //、
                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
                    }
                }
                bucket.push([key,value])
                this.count+=1
            }
            HashTable.prototype.get=function(key){
                //
                var index=this.hashFunc(key,this.limit)
                //根据index取出对应的bucket
                var bucket=this.storage[index]
                if(bucket==null){
                    return null
                }
                for(var i=0;i<bucket.length;i++){
                    var tuple=bucket[i]
                    if(tuple[0]==key){
                       return tuple[1]
                    }
                }
                return null
            }
            HashTable.prototype.remove=function(key){
                var index=this.hashFunc(key,this.limit)
                //根据index取出对应的bucket
                var bucket=this.storage[index]
                if(bucket==null){
                    return null
                }
                for(var i=0;i<bucket.length;i++){
                    var tuple=bucket[i]
                    if(tuple[0]==key){
                    bucket.splice(i,1)
                    this.count--
                    return tuple[1]
                }
                }
              return null
            }
        }
    </script>
</body>
</html>
相关文章
|
17天前
|
存储 编译器 C语言
【数据结构】C语言实现链队列(附完整运行代码)
【数据结构】C语言实现链队列(附完整运行代码)
33 0
|
17天前
|
存储 算法 程序员
【数据结构】C语言实现顺序表万字详解(附完整运行代码)
【数据结构】C语言实现顺序表万字详解(附完整运行代码)
35 0
|
19天前
|
算法
数据结构-哈希表(二)
数据结构-哈希表(二)
30 0
|
1月前
【数据结构】数组、双链表代码实现
【数据结构】数组、双链表代码实现
|
1月前
|
存储 索引 Python
python中的哈希表数据结构
python中的哈希表数据结构
14 0
|
19天前
|
存储 缓存 Serverless
数据结构-哈希表(一)
哈希表(Hash Table),也称为散列表,是一种常见的数据结构,用于存储键值对。它通过将键映射到一个特定的索引位置来实现高效的数据访问和查找。
20 3
|
存储 算法 安全
【C/C++ 数据结构 】从零开始实现哈希表:C++实践指南
【C/C++ 数据结构 】从零开始实现哈希表:C++实践指南
68 0
|
22天前
|
机器学习/深度学习 存储 Java
揭秘数组:数据结构的基石与代码实践解析
揭秘数组:数据结构的基石与代码实践解析
8 0
|
22天前
|
存储 编译器
数据结构之顺序表的实现(详解!附完整代码)
数据结构之顺序表的实现(详解!附完整代码)
35 1
|
23天前
|
存储 机器学习/深度学习 算法
C语言代码实现数据结构与算法
以上代码中,哈希表使用链表解决哈希冲突,每个链表节点包含一个键值对。hash函数用于计算键值对应的哈希值,insert函数用于向哈希表中插入一个键值对,若当前位置为空,则直接插入;否则,将新节点插入到链表末尾。search函数用于在哈希表中查找指定键值的值,若存在则返回其值,否则返回-1。
32 1