Javascript之数据结构与算法的HashMap实现

本文介绍了如何在JavaScript中自实现HashMap,引用了LinkedList的实现方式,详细探讨了HashMap的数据结构和算法原理。

Javascript之数据结构与算法的HashMap实现

1.自实现HashMap

let LinkedList=require("./LinkedList")
let loseloseHashCode=Symbol();
class HashMap{
   
   
    constructor(){
   
   
        this.table=[];
        this[loseloseHashCode]=function(key){
   
   //散列函数
            let hash=0;
            for(let i=0;i<key.length;i++){
   
   
                hash+=key.charCodeAt(i);//字符对应的asc码值
            }
            return hash%37;
        };//私有函数
        this[djb2HashCode]=function(key){
   
   //优化版散列函数
            let hash=5381;
            for(let i=0;i<key.length;i++){
   
   
                hash=hash*33+key.charCodeAt(i);
            }
            return hash%1013;
        }
        
    }
    //分离链接法
    put(key,value){
   
   
        let position=this[loseloseHashCode](key);

        if(this.table[position]==undefined){
   
   
            this.table[position]=new LinkedList();
            this.table[position].append(new ValuePair(key,value));  
        }else if(this.table[position]!=undefined){
   
   
            let hasKey=false;
            let current=this.table[positi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值