• JS 列表


    定义

    列表的抽象定义

    • listSize 描述列表的长度
    • pos 列表当前的位置
    • length 返回列表的元素的个数
    • clear() 清空列表
    • toString() 返回列表的字符串
    • getElement() 返回当前位置的元素
    • insert() 在现有元素后面插入元素
    • append() 在列表末尾添加元素
    • remove() 从列表中删除元素
    • front() 将列表的当前位置移动到一个位置
    • end() 将列表的当前位置移动到最后位置
    • prev() 将当前位置前移一位
    • next() 将当前位置后移一位
    • currPos() 放回列表当前位置
    • moveTo() 移动到指定位置
    function list() {
        this.listSize = 0;
        this.pos = 0;
        this.dataStore = []; // 初始化一个空数组来保存列表元素
    
        this.find = find;
        this.toString = toString;
        this.append = append;
        this.remove = remove;
        this.insert = insert;
        this.clear = clear;
    
        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.contains = contains;
    }
    
    // 元素空间添加一个 this.listSize++
    function append(item) {
        this.dataStore[this.listSize++] = item;
    }
    
    
    function find(item) {
        for (var index = 0; index < this.listSize; index++) {
            var element = this.dataStore[index];
            if (element == item)
                return index;
        }
        return -1;
    }
    
    // 数组listSize -1 ,找到元素删除
    function remove(item) {
        var findAt = this.find(item);
        if (findAt > -1) {
            this.listSize--;
            this.dataStore.splice(findAt, 1);
            return true;
        }
        return false;
    }
    
    function length() {
        return this.listSize;
    }
    
    
    function toString() {
        return this.dataStore;
    }
    
    // 将新元素插入在某个元素之后
    function insert(element, after) {
        //1:找到某个元素
        var findAt = this.find(after);
        if (findAt == -1) {
            return false;
        }
        this.listSize++;
        this.dataStore.splice(findAt + 1, 0, element);
        return true;
    }
    
    function clear() {
        delete this.dataStore;
        this.dataStore = [];
        this.listSize = this.pos = 0;
    }
    
    // 检查是否包含某个元素
    function contains(item) {
        return this.dataStore.indexOf(item) > -1;
    }
    
    // 移动到第一个位置
    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 + 1 <= this.listSize)
            this.pos++;
    }
    
    
    // 返回当前位置
    function currPos() {
        return this.pos;
    }
    
    // 移动到指定位置
    function moveTo(position) {
        this.pos = position;
    }
    
    // 返回当前位置的元素
    function getElement() {
        return this.dataStore[this.pos];
    }
  • 相关阅读:
    测试如何发挥更大的价值?聊聊测试左移和右移
    Cocos Creator性能调优
    跨域问题产生的原因和解决方法
    tornado部署
    tonado
    MySQL binlog
    grpc
    nextjs中的懒加载
    前端低代码-少写代码实现灵活需求
    MySQL中的锁
  • 原文地址:https://www.cnblogs.com/dark-liu/p/5785235.html
Copyright © 2020-2023  润新知