zoukankan      html  css  js  c++  java
  • JavaScript链表

        //实现列表类
        function list() {
            this.listSize = 0;//元素个数 属性
            this.pos = 0;//当前位置 属性
            this.length = length;//个数 属性
            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.currPos = currPos;//返回当前位置
            this.moveTo = moveTo;//移动到指定位置
            this.getElement = getElement;//返回当前位置的元素
            this.contains = contains;//匹配是否相似
        }
     
        //添加
        function append(element) {
            this.dataStore[this.listSize++] = element;
        }
     
        //查找列表中某一元素
        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 length() {
            return this.listSize;
        }
     
        //显示列表元素
        function toString() {
            return this.dataStore;
        }
       
        //插入一个元素
        function insert(element, after) {
            //查找到传入元素的位置
            var insertPos = this.find(after);
            if(insertPos >-1)
            {
                //分割数组,在首位插入值
                this.dataStore.splice(insertPos, 0, element);
                //记录列表大小
                ++this.listSize;
                return true;
            }
            return false;
        }
     
        //清空列表
        function clear() {
            //delete操作符删除数组dataStore 
            delete this.dataStore;
            //创建一个空数组
            this.dataStore = [];
            //将数组大小及当前位置设置为首位
            this.listSize = this.pos = 0;
        }
     
        //判断给定值是否在列表中
        function contains(element) {
            //遍历列表,判断给定值是否存在
            for (var i = 0; i < this.dataStore.length; ++i) {
                if (this.dataStore[i] == element) {
                    return true;
                }
            }
            return false;
        }
     
        //允许用户在列表上自由移动
     
        //将当前位置移到第一个元素
        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("1");
        names.append("2");
        names.append("3");
        names.append("4");
        names.append("5");
        //names.remove("3");
        //names.front();
        //names.next();
    好好学习,天天向上。
  • 相关阅读:
    SQLite在iOS开发中的使用
    实现序列化和反序列化
    NSPredicate用于对集合类中的元素进行筛选
    通知中心NSNotification与委托的异同,需要注意的要点
    OC 重写description,isEqual方法
    iOs 单例模式的定义,实现、步骤
    Obejctiv-c 里面KVC 和 KVO的实现步骤,和有关方法
    关于NSTimer的几种构建方式
    NSTimer 实现到一个指定时间(年、月、日)的倒计时
    关于UILable、UIButton、UITextField简单运用
  • 原文地址:https://www.cnblogs.com/Zhengxue/p/6141397.html
Copyright © 2011-2022 走看看