美文网首页前端技术讨论
javaScript数据结构--散列表

javaScript数据结构--散列表

作者: 安然_她 | 来源:发表于2019-05-14 15:30 被阅读0次

散列集合是由一个集合构成,但是插入、移除、或获取元素时,使用的是散列函数

散列表的代码实现

// 散列表

function LinkedList() {

    var Node = function(element) {

        this.element = element;

        this.next = null;

    }

    var length = 0;

    var head = null;

    this.append = function(element) {

        const node = new Node(element);

        let current ;

        if(head === null) {

            head = node;

        } else {

            current = head;

            while(current.next) {

                current = current.next;

            }

            current.next = node;

        }

        length ++;

    }

    this.insert = function(position, element) {

        if(position >= 0 && position <= length) {

            const node = new Node(element);

            let current = head,

                previous,

                index = 0;

            if(position === 0) {

                head.next = current;

                head = node;

            } else {

                while(index++ < position) {

                    previous = current;

                    current = current.next;

                }

                previous.next = node;

                node.next = current;     

            }

            length++;

            return true;

        } else {

            return false;

        }

    }

    this.remove = function(element) {

        const index = this.indexOf(element);

        return this.removeAt(index);

    }

    this.removeAt = function(position) {

        if(position >= 0 && position <= length) {

            var current = head,

                previous,

                index = 0;

            if(position === 0) {

                head = current.next;

            }else {

                while(index++ < position) {

                    previous = current;

                    current = current.next;

                }

                previous.next = current.next;

            }

            length--;

            return current.element;

        }else {

            return null;

        }

    }

    this.isEmpty = function() {

        return length === 0

    }

    this.size = function() {

        return length;

    }

    this.toString = function() {

        var current = head,

            string = '';

        while(current) {

            string += String(current.element);

            current = current.next;

        }

        return string;

    }

    this.indexOf = function(element) {

        var current = head,

            index = -1;

        while(current) {

            index++;

            if(current.element === element) {

                return index;

            }

            current = current.next;

        }

        return -1;

    }

    this.getHead = function() {

        return head;

    }

}

var l = new LinkedList();

l.append("a");

l.append("b");

l.append("c");

console.log(l.size()); // 3

console.log(l.toString()); // abc

l.insert(1, "d")

console.log(l.size()); // 4

console.log(l.toString()); // adbc

console.log(l.indexOf("e")); // -1

console.log(l.indexOf("b")); // 2

l.removeAt(2);

console.log(l.toString()); // adc

l.remove("a"); // dc

console.log(l.toString());

l.remove("dd");

console.log(l.toString()); // dc

// 有时候, 一些键会有相同的散列值,不同的值在散列表中处于同一个位置,会遇到冲突。

处理冲突方式 分离链接、线性探查

分离链接的代码实现方式:

function HashTable() {

    var table = [];

    var loseloseHashCode = function(key) {

        var hash = 0;

        for(var i=0; i<key.length; i++) {

            hash += key.charCodeAt(i);

        }

        return hash % 37;

    }

    var ValuePair = function(key, value) {

        this.key = key;

        this.value = value;

        this.toString = function() {

            return '[' + this.key + '-' + this.value + ']';

        }

    }

    this.put = function(key, value) {

        var position = loseloseHashCode(key);

        if(table[position] == undefined) {

            table[position] = new LinkedList();

        }

        table[position].append(new ValuePair(key, value));

    }

    this.remove = function(key) {

        const position = loseloseHashCode(key);

        if(table[position] !== undefined) {

            const list = table[position];

            list.remove(key);

            return true;

        }

        return false;

    }

    this.get = function(key) {

        const position = loseloseHashCode(key);

        if(table[position] !== undefined) {

            const current = table[position].getHead();

            while(current.next) {

                if(current.element.key === key) {

                    return current.element.value;

                }

                current = current.next;

            }

            if(current.element.key === key) {

                return current.element.value

            }

        }

        return undefined;

    }

}

//线性探查

function HashTable() {

    var table = [];

    var loseloseHashCode = function(key) {

        var hash = 0;

        for(var i=0; i<key.length; i++) {

            hash += key.charCodeAt(i);

        }

        return hash % 37;

    }

    var ValuePair = function(key, value) {

        this.key = key;

        this.value = value;

        this.toString = function() {

            return '[' + this.key + '-' + this.value + ']';

        }

    }

    this.put = function(key, value) {

        var position = loseloseHashCode(key);

        if(table[position] == undefined) {

            table[position] = new ValuePair(key, value);

        }else {

            var index = ++position;

            while(table[index != undefined]) {

                index ++;

            }

            table[index] = new ValuePair(key, value);

        }

    }

    this.remove = function(key) {

        const position = loseloseHashCode(key);

        if(table[position] !== undefined) {

            if(table[position].key === key) {

                table[position] = undefined;

                return true;

            }else {

                var index = position;

                while(table[index] === undefined || table[index].key !== key ) {

                    index++;

                }

                if(table[index].key !== key) {

                    table[position] = undefined;

                    return true;

                }

            }

        }

        return false;

    }

    this.get = function(key) {

        const position = loseloseHashCode(key);

        if(table[position] !== undefined) {

            if(table[position].key === key) {

                return table[position].value;

            }else {

                var index = ++position;

                while(table[index] === undefined || table[index] .key !== key) {

                    index++;

                }

                if(table[index].key === key) {

                    return table[index].value;

                }

            }

        }

        return undefined;

    }

}

相关文章

  • javaScript数据结构--散列表

    散列集合是由一个集合构成,但是插入、移除、或获取元素时,使用的是散列函数 散列表的代码实现 // 散列表 func...

  • 数据结构——散列

    散列也叫作散列表,是一种非线性的数据结构,可以用来快速的查找和插入数据。在 JavaScript 中,通常使用数组...

  • 学习JavaScript数据结构与算法(第2版)

    二维和多维数组 栈数据结构 队列数据结构(排队) 链表数据结构 双向链表 集合 字典和散列表 散列表 树 二叉树 ...

  • 算法图解-散列表

    1. 散列表 散列表由键和值组成,散列表将键映射到值。 在复杂数据结构中,散列表可能是最有用的,也被称为散列映射、...

  • 散列表

    什么是散列表 散列表也叫哈希表,输入某一关键字输出其对应的数值的数据结构 散列表的生成依赖于散列函数,散列函数的要...

  • 散列表下

    为什么散列表和链表经常会一起使用? 散列表这种数据结构虽然支持非常高效的数据插入、删除、查找操作,但是散列表中的数...

  • 散列表下

    为什么散列表和链表经常会一起使用? 散列表这种数据结构虽然支持非常高效的数据插入、删除、查找操作,但是散列表中的数...

  • 基础概念

    散列表 散列表是一种将键(Key)映射为值(Value)从而快速查找的数据结构 散列表包含一个底层数组和一个散列函...

  • Java HashMap原理解析

    本文分析HashMap的实现原理。 数据结构(散列表) HashMap是一个散列表(也叫哈希表),用来存储键值对(...

  • JavaScript 数据结构之哈希表(散列表)

    一、 哈希表的介绍 1. 哈希表的优势 哈希表通常是基于数组实现的,但相对于数组,它有很多优势 快速的插入、删除、...

网友评论

    本文标题:javaScript数据结构--散列表

    本文链接:https://www.haomeiwen.com/subject/jwrsaqtx.html