javascript 数据结构系列

本系列一系列文章的网络关于javascript中的数据构造

假如你对数据构造不太熟悉 能够看这篇文章
假如你只想看代码 能够看这篇文章
假如你只想star 能够去这里

什么是数据构造

在盘算机科学或信息学中,数据构造(英语:data structure)是盘算机中存储、构造数据的体式格局 From Wikipedia

完全wiki诠释地点

没有一种数据构造是圆满的 你须要相识一切数据构造的上风和劣势 合理的应用数据构造完成本身目的

复杂性

算法的复杂性是算法上风和劣势的一种表现 算法复杂性包含空间复杂性和时候复杂性

空间复杂性也许也许就是数据构造所须要运用的内存数目
时候复杂性也许也许就是时候啦

怎样挑选适宜的算法

依靠挑选的数据

《javascript 数据结构系列》
《javascript 数据结构系列》

array

An Array data structure, or simply an Array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. The simplest type of data structure is a linear array, also called one-dimensional array. From Wikipedia

js 有array构造不过不是很壮大 我们能够自定义一下一个加强版

class MyArray {
    constructor() {
        this.array = [];
    }

    add(data) {
        return this.array.push(data);
    }

    remove(data) {
        // 假如找到数据就删除
        if(~this.array.indexOf(data)) {
            this.array.splice(this.array.indexOf(data), 1);
        }
    }

    search(data) {
        // 假如找打数据就返回
        if(~this.array.indexOf(data)) {
            return this.array.indexOf(data);
        } else {
            return null;
        }
    }

    getAtIndex(index) {
        return this.array[index];
    }

    length() {
        return this.array.length;
    }

    print() {
        console.log(this.array.reduce(function(prev, curr) {
            return prev + curr + ' ';
        }, '').trim());
    }
}

hashtable

散列表Hash table,也叫哈希表),是依据关键字(Key value)而直接接见在内存存储位置的数据构造。也就是说,它经由过程盘算一个关于键值的函数,将所需查询的数据映照到表中一个位置来接见纪录,这加快了查找速率。这个映照函数称做散列函数,寄存纪录的数组称做散列表

《javascript 数据结构系列》

class HashTable {
    constructor(size) {
        this.values = {};
        this.numberOfValues = 0;
        this.size = size;
    }
    add(key, value) {
        var hash = this.calculateHash(key);
        // 假如对象没有该key  实例化该key处为一对象
        if(!this.values.hasOwnProperty(hash)) {
            this.values[hash] = {};
        }
        // 假如该key不存在 就增添一个数目
        if(!this.values[hash].hasOwnProperty(key)) {
            this.numberOfValues++;
        }
        this.values[hash][key] = value;
    }
    remove(key) {
        var hash = this.calculateHash(key);
        if(this.values.hasOwnProperty(hash) &&  this.values[hash].hasOwnProperty(key)) {
            delete this.values[hash][key];
            this.numberOfValues--;
        }
    }
    calculateHash(key) {
        return key.toString().length % this.size;
    }
    search(key) {
        var hash = this.calculateHash(key);
        if(this.values.hasOwnProperty(hash) &&  this.values[hash].hasOwnProperty(key)) {
            return this.values[hash][key];
        } else {
            return null;
        }
    }
    length() {
        return this.numberOfValues;
    }
    print() {
        var string = '';
        for(var value in this.values) {
            for(var key in this.values[value]) {
                string += this.values[value][key] + ' ';
            }
        }
        console.log(string.trim());
    }
}
    原文作者:andypinet
    原文地址: https://segmentfault.com/a/1190000004558235
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞