zoukankan      html  css  js  c++  java
  • 实现列表项:find()&insert()&remove()&contains()&clear()

        function List() {
            this.listSize = 0;//列表的元素个数,属性
            this.pos = 0;//列表的当前位置,属性
            this.dataStore = []; // 初始化一个空数组来保存列表元素
    
            this.length = length;//返回列表中元素的个数
            this.toString = toString;//返回列表的字符串形式,方法
    
            this.append = append;//在列表的末尾添加新元素,方法
    
            this.find = find;//查找元素的位置,用于辅助    
            this.insert = insert;//在现有元素后插入新元素,方法    
            this.remove = remove;//从列表中删除元素,方法    
            this.contains = contains;
    
            this.clear = clear;//清空列表中的所有元素,方法
        }
    
        function length() {
            return this.listSize;
        }
        function toString() {
            return this.dataStore;
        }
    
        function append(element) {
            this.dataStore[this.listSize++] = element;
            //后自加,在新位置添加元素,同时列表的元素个数加1
        }
    
        function find(element) {
            for ( var i = 0; i < this.dataStore.length; ++i) {
                if (this.dataStore[i] == element) {
                    return i;
                }
            }
            return -1;
        }
        function remove(element) {
            var foundAt = this.find(element);
            if (foundAt > -1) {
                this.dataStore.splice(foundAt, 1);
                --this.listSize;
                return true;
            }
            return false;
        }
        function insert(element, after) {
            var insertPos = this.find(after);
            if (insertPos > -1) {
                this.dataStore.splice(insertPos + 1, 0, element);
                ++this.listSize;
                return true;
            }
            return false;
        }
        function contains(element) {
            for ( var i = 0; i < this.dataStore.length; ++i) {
                if (this.dataStore[i] == element) {
                    return true;
                }
            }
            return false;
        }
    
        function clear() {
            delete this.dataStore;
            this.dataStore = [];
            this.listSize = this.pos = 0;
        }
        
        var names = new List();
        names.append("Cynthia");
        names.append("Raymond");
        names.append("Barbara");
        alert(names.toString());
        names.remove("Raymond");
        alert(names.toString());
  • 相关阅读:
    记录下平台多种语言加密算法实施的历程
    Php AES加密、解密与Java互操作的问题
    Tomcat服务器常用配置和HTTP简介
    淘宝IP地址查询
    linux下hexdump和od命令:显示文件十六进制格式
    技术讨论 | 简谈渗透测试各阶段我常用的那些“神器”
    串口发送数据速度
    在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么?
    C++ 函数参数中“ *&代表什么? ”
    c++中三种参数引用方式
  • 原文地址:https://www.cnblogs.com/feile/p/5371194.html
Copyright © 2011-2022 走看看