数据结构一:列表

227 阅读2分钟

列表

列表的概念和用途

  • 日常生活中,我们使用购物清单、待办事项都是列表。计算机中的列表也是一样
  • 元素不是很多
  • 不需要很长序列查找元素或者排序
  • 列表是一种最自然的数据组织方式

列表关键概念定义

  • 列表是一组有序的数据。每个列表中的数据项称为元素。元素的数量受内存控制
  • 不包含任何元素的列表称为空列表

迭代器的优点

  • 访问元素时不必关心底层数据结构(不用考虑他是树还是别的啥的)
  • 增加和删除元素要比 for 更加灵活
  • 迭代器访问列表里的元素提供了统一的方法
class List {
    constructor() {
        this.dataStore = []; //初始化一个空数组用来保存列表元素
        this.listSize = 0; //列表当前位置
        this.postion = 0; //列表当前位置
    }

    // 列表包含元素的个数
    length() {
        return this.listSize;
    }

    // 表示指定的数组及其元素的字符串 [1,"a"]=>"1,a"
    toString() {
        return this.dataStore.toString();
    }

    // 在列表元素末尾增加新元素
    append(element) {
        this.dataStore[this.length() + 1] = element;
        this.listSize++;
    }

    // 在列表元素首部增加新元素
    appendBefore(element) {
        for(let i =this.length();i>0;i--){
            this.dataStore[i]=this.dataStore[i-1]
        }
        this.dataStore[0] = element;
        this.listSize++;
    }

    // 查找元素
    find(element) {
        for (let i = 0; i < this.length(); ++i) {
            if (this.dataStore[i] === element) {
                return i;
            }
        }
        return -1;
    }

    // 从列表中删除元素
    remove(element) {
        const foundAt = this.find(element);
        if (foundAt > -1) {
            this.dataStore.splice(foundAt, 1);
            this.listSize--;
            return;
        }
        return false;
    };

    // 在现有元素后插入新元素
    insert(currentElement, newElement) {
        const pos = this.find(currentElement);
        if (pos > -1) {
            this.dataStore.splice(pos + 1, 0, newElement);
            this.listSize++;
            return true;
        }
        return false;
    }

    // 在现有元素前插入新元素
    insertBefore(currentElement, newElement) {
        const pos = this.find(currentElement);
        if (pos > -1) {
            this.dataStore.splice(pos, 0, newElement);
            this.listSize++;
            return true;
        }
        return false;
    }

    //从列表的当前位置移动到第一个元素
    front() {
        this.postion = 0;
    };

    //从列表的当前位置移动到最后一个位置
    end() {
        this.postion = this.length() - 1;
    };

    //将当前位置后移一位
    next() {
        if (this.postion !== this.length() - 1) {
            this.postion++;
        }
    }

    //将当前位置前移一位
    prev() {
        if (this.postion !== 0) {
            this.postion--;
        }
    }

    //返回列表当前位置
    currPos() {
        return this.postion;
    }

    // 将当前位置移动到指定位置
    moveTo(index) {
        if (index >= 0 && index <= this.length) {
            this.postion = index;
            return true;
        }
        return false;
    }

    //清空列表中的所有元素
    clear() {
        this.dataStore = [];
        this.listSize = 0;
        this.postion = 0;
    }

    // 显示当前的元素
    getElement(index) {
        return this.dataStore[index];
    }

    //是否包含该元素
    contains(element) {
        for (let i=0;i<this.length();i++) {
            if(this.dataStore[i]===element){
                return true;
            }
        }
        return false;
    }
}