12.2 【HashMap】js 利用线性探查解决散列冲突问题

再次展示了js动态数组的方便性

'use strict';

// 内部类,定义链表节点内容
class valuePire {
    constructor (key, value) {
        this.key = key;
        this.value = value
    
        this.toString = function () {
            return '[ k:' + this.key + 'value: ' + this.value + ']'
        }
    } 
}
class HashMap {
    constructor () {
        this.table = []
    }
    // 散列函数
    loseloseHashCode (key) {
        let hash = 0;
        for(let i = 0; i < key.length; i++) {
            hash += key[i].charCodeAt()
        }
        return hash % 37
    }


    // 存放
    put (key, value) {
        let position = this.loseloseHashCode(key)

        if (this.table[position] === undefined) {
            this.table[position] = new valuePire(key, value)
        } else {
            let index = ++ position
            while (this.table[index] !== undefined) {
                index ++
            }
            this.table[position] = new valuePire(key, value)
        }
    }

    remove (key) {
        let position = this.loseloseHashCode(key)
        // console.log(this.table[position].key);
        if (this.table[position] !== undefined) {
            if (this.table[position].key === key) {
                this.table[index] = undefined
            } else {
                let index = ++ position
                while (this.table[index] === undefined
                    || this.table[index].key !== key) {
                    index ++
                }
                if (this.table[index].key === key) {
                    this.table[index] = undefined
                }
            }
        }
        return undefined
    }

    get (key) {
        let position = this.loseloseHashCode(key)
        // console.log(this.table[position].key);
        if (this.table[position] !== undefined) {
            if (this.table[position].key === key) {
                return this.table[position].value
            } else {
                let index = ++ position
                while (this.table[index] === undefined
                    || this.table[index].key !== key) {
                    index ++
                }
                if (this.table[index].key === key) {
                    return this.table[index].value
                }
            }
        }
        return undefined
    }
}

let hashmap = new HashMap();
hashmap.put('Gandalf', 'gandalf@email.com')

hashmap.put('Tyrion', 'tyrion@email.com')
hashmap.put('Aaron', 'Aaron@email.com')

hashmap.put('Donnie', 'Donnie@email.com')
hashmap.put('Ana', 'Ana@email.com')

hashmap.put('Jonathan', 'Jonathan@email.com')
hashmap.put('Jamie', 'Jamie@email.com')
hashmap.put('Sue', 'Sue@email.com')
// hashmap.remove('Jamie')
let temp = hashmap.get('Jamie')
console.log(temp);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,374评论 0 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • JavaScript语言精粹 前言 约定:=> 表示参考相关文章或书籍; JS是JavaScript的缩写。 本书...
    微笑的AK47阅读 599评论 0 3
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,275评论 0 4
  • 站在大海边 才发现自己多渺小 登上最高山 才发现天有多高 ...
    辉沙言柔阅读 456评论 1 11