zoukankan      html  css  js  c++  java
  • 使用迭代器遍历、访问列表

    使用迭代器遍历、访问列表

    function List() {
        this.listSize = 0;//列表中元素的个数
        this.dataStore = [];//初始化一个空数组来保存数据
        this.clear = clear;//清空列表中的所有元素
        this.find = find;//在列表中查找某一元素
        this.toString = toString;//显示列表中的元素
        this.insert = insert;//向列表中插入一个元素
        this.append = append;//给列表添加元素
        this.remove = remove;//从列表中删除元素
        this.contains = contains;//判断列表中是否存在某一元素
        this.length = length;//列表中有多少个元素
        this.pos = 0;//列表当前元素所在位置
        this.front = front;//将列表当前位置移到第一个位置
        this.end = end;//将列表当前位置移到最后一个位置
        this.prev = prev; //将列表当前位置向前移动一个位置
        this.next = next;//将列表当前位置向后移动一个位置
        this.currPos = currPos;//列表的当前位置
        this.moveTo = moveTo;//将列表当前位置移动到某个位置
        this.getElement = getElement;//获取当前位置的元素
    }
    function clear(){
        delete this.dataStore;
        this.dataStore = [];
        this.listSize = 0;
        this.pos = 0;
    }
    
    function find(element) {
        for (var i = 0; i < this.dataStore.length; ++i) {
            if (this.dataStore[i] == element) {
                return i;
            }
        }
        return -1;
    }
    
    function toString(){
        return this.dataStore;
    }
    
    function insert(element,after){
        var insertPos = this.find(after);
        if(insertPos > -1){
            this.dataSore.splice(insertPos+1,0,element);
            this.listSize++;
            return true;
        }
        return false;
    }
    
    function append(element) {
        this.dataStore[this.listSize++] = element;
    }
    
    function remove(element) {
        var foundAt = this.find(element);
        if (foundAt > -1) {
            this.dataStore.splice(foundAt,1);
            this.listSize--;
            return true;
        }
        return false;
    }
    
    function contains(){
        for(var i= 0; i < this.dataStore.length;i++){
            if(this.dataStore[i] == element){
                return true;
            }
        }
        return false;
    }
    
    function length(){
        return this.listSize;
    }
    
    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];
    }
    var names = new List();
    names.append("Cynthia");
    names.append("Raymond");
    names.append("Barbara");
    names.append("amrbara");
    names.append("zmrbara");
    names.append("wmrbara");
    console.log(names.toString());//[ 'Cynthia', 'Raymond', 'Barbara', 'amrbara', 'zmrbara', 'wmrbara' ]
    names.remove("Raymond");
    console.log(names.toString());//[ 'Cynthia', 'Barbara', 'amrbara', 'zmrbara', 'wmrbara' ]
    
    /*使用迭代器访问列表*/
    for(names.front();names.currPos() <= names.length();names.next()){
        console.log(names.getElement());
        if(names.pos == names.length()-1){
            return false;
        }
    }
  • 相关阅读:
    Linux基础(一)
    计算机基础之网络基础
    计算机基础之操作系统
    计算机基础之计算机硬件基础
    .NET Reflector 8.2支持VS2013高亮显示和代码地图视图
    jQuery图表开源软件
    8个实用的页面布局和用户界面jQuery插件
    关于mvc3.0RadioButtonFor的使用
    链式调用-python
    给实数用千分位方法输出结果
  • 原文地址:https://www.cnblogs.com/baiyangyuanzi/p/6673194.html
Copyright © 2011-2022 走看看