数据结构的那些事(一)

前端早已不是当年的切图仔了。想要升职加薪,进军大厂,在前端的路上走的更远,你不得不了解一下数据结构。前端

经常使用的数据结构有:数组,列表,栈,队列,链表,字典,散列表,树,图。

咱们会在接下来一个一个的介绍,而且实现它们!数组

1.数组:最多见也是用的最多的数据结构就是数组了。它经过索引来实现元素的任意存储。

大多数语言都有数组,值得一提的是,JavaScript中的数组是一种特殊的对象,所以索引在内部会被转换成字符串类型,因此效率会有所折扣。bash

由于数组你们了解的比较多,在这里只提下数组的迭代器方法:

不生成新数组的迭代器方法:数据结构

forEach():接收一个函数做为参数,对数组中每一个元素使用该函数
every():接收一个返回值为布尔值的函数,遍历数组,对于全部函数返回true,该方法返回true
some():接收一个返回值为布尔值的函数,遍历数组,只要有一个函数返回true,该方法返回true
reduce():使用接收的函数操做数组,最后返回一个结果
复制代码

生成新数组的迭代器方法app

map()
filter()
复制代码

2.列表:适用于保存元素很少,不须要在一个很长的序列中查找元素,或对其排序。

列表是一组有序的数据,每一个列表中的数据项称为元素。 首先咱们来设计一下这个列表应该具备哪些属性和方法:函数

function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = []; // 初始化一个空数组来保存列表元素
    this.clear = clear;
    this.find = find;
    this.toString = toString;
    this.insert = insert;
    this.append = append;
    this.remove = remove;
    this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
    this.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.length = length;
    this.contains = contains;
}
复制代码

当咱们设计好一个列表类后,接下来开始实现里面的方法:ui

//append
function append(e){
    this.dataStore[this.listSize++] = e;
}
//remove方法要求咱们首先要找到删除的元素,咱们须要一个辅助方法find()
function find(e){
    for(let i = 0; i<this.dataStore.length; ++i){
        if(this.dataStore[i] === e){
            return i
        }
    }
    return -1
}
function remove(e){
    let findCode = this.find(e);
    if(indCode > -1){
        this.dataStore.splice(findCode, 1);
        --this.listSize;
        return true;
    }
    return false;
}
//insert 向列表中插入一个元素
function insert(e, where){
    let insetPos = this.find(where);
    if(insertPos > -1){
        this.datastore.splice(insertPos+1, 0 , e);
        ++this.listSize;
        return true;
    }
    return false;
}
//length
function length(){
    return this.listSize;
}
//清空列表中的元素
function clear(){
    delete this.dataStore;
    this.dataStore = [];
    this.listSize = this.pos = 0;
}
//cotains判断给定值是否在列表中
function coatains(e){
    for(let i = 0; i<this.dataStore.length; ++i ){
        if(this.dataStore[i] === e){
            return true;
        }
    }
    return false;
}
复制代码

最后,咱们还须要一组方法来遍历列表this

function front() {
    this.pos = 0;
}
function end() {
    this.pos = this.listSize-1;
}
function prev() {
    if (this.pos > 0) {
        --this.pos;
    }
}
function next() {
    if (this.pos < this.listSize-1) {
        ++this.pos;
    }
}
function currPos() {
    return this.pos;
}
function moveTo(position) {
    this.pos = position;
}
function getElement() {
    return this.dataStore[this.pos];
}
复制代码

到这里,咱们就本身实现了一个列表类。有本身的属性,能够增删改查,能够遍历。spa

3.栈:栈是和列表相似的一种数据结构,可是它遵循后进先出的规则

由于数据只能在栈顶添加或删除,因此这样的操做很快,并且容易实现。 对栈的两种主要操做是将一个元素压入栈和讲一个元素弹出栈,另外一个经常使用操做是访问栈顶元素。 同样的,在实现以前,咱们先来设计一下栈应该具备的属性和方法:设计

function Stack() {
    this.dataStore = [];//存储
    this.top = 0;//栈顶
    this.push = push;//入栈操做
    this.pop = pop;//出栈操做
    this.peek = peek;//获取栈顶元素
}
复制代码

接下来咱们来实现这些方法:

function push(e){
    this.dataStore[this.top++] = e;
}

function pop(){
    return this.dataStore(--this.top);
}

function peek(){
    return this.dataStore[this.top - 1];
}
复制代码

有时候须要知道栈内存储元素个数,能够定义一个length方法:

function length(){
    return this.top
}
复制代码

清空栈也很简单:

function clear(){
    this.top = 0;
}
复制代码
适合使用栈的场景:

(1)数制间的相互转换

eg:将数字转换为二进制和八进制
function changeBase(num, base){
    let s = new Stack();
    do{
        s.push(num % base);
        num = Math.floor(num /= base);
    }while(num > 0)
    let convert = "";
    while(s.length() > 0){
        convert += s.pop();
    }
    return convert;
}

复制代码

(2)回文:回文指从前日后写和从后往前写都是同样的结构。使用栈能够轻松地判断一个字符串是不是回文。

字符串完整压入栈内后,经过持续弹出栈中的每一个字母就能够获得一个新的字符串,该字符串恰好与原来的字符串顺序相反。咱们只须要比较这连个字符串是否同样便可判断是否是回文。

(3)用栈来模拟递归

function fact(n){
    let s = new Stack();
    while(n > 1){
        s.push(n--);
    }
    let product = 1;
    while(s.length() > 0){
        product *= s.pop();
    }
    return product;
}
复制代码

避免一篇博客过长,我会将数据结构分红几篇来写,喜欢的帮忙点个赞,给博主一点动力~~

相关文章
相关标签/搜索