列表

作者: _____西班木有蛀牙 | 来源:发表于2018-06-28 00:47 被阅读3次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>列表</title>
</head>

<body>
    <script>
        function List() {
            // 列表元素个数
            this.listSize = 0;
            // 列表当前的位置
            this.pos = 0;
            // 初始化一个空数组用来保存列表元素
            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.length = length;
            // 返回列表当前的位置
            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.slice(foundAt, 1);
                --this.listSize;
                return
            }
            return false
        }

        function length() {
            return this.listSize;
        }

        function toString() {
            return this.dataStore;
        }

        function insert(after) {
            var insertPos = this.find(after);
            if (insertPos > -1) {
                this.dataStore.splice(insertPos + 1, 0, element);
                ++this.listSize;
                return true;
            }
            return false;
        }

        function clear() {
            delete this.dataStore;
            this.dataStore.length = 0; // 创建一个空数组
            this.listSize = 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) {
                ++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('小红');
        names.append('小王');
        names.append('小丽');
        names.next();
        alert(names.getElement());
        // 迭代器
        for (names.front();names.currPos() < names.length();names.next()) {
            console.log(names.getElement());
        }
    </script>
</body>

</html>

相关文章

  • Markdown 系列(三) 列表

    无序列表 由圆点组成的列表 列表1 列表2 列表3 列表1 列表2 列表3 列表1 列表2 列表3 +-*这三种符...

  • markdown常用的语法

    列表 有序列表: 列表项 1 列表项 2 无序列表: 列表项 1 列表项 2 列表项 3 列表项 4 列表项缩进两...

  • markdown常用语法

    标题 列表 无序列表- 列表1 - 列表1.1 -列表1.2- 列表2 有序列表1. 列表1 1. 列表1....

  • html阶段第二节第一天

    高级标签 列表标签 无序列表 列表一 列表二 ...... 有序列表 列表一 列表二 ...... 定义列表dl...

  • markdown测试

    段落 三级标题 四级标题 五级标题 列表 无序列表 列表1 列表2 列表3 列表1 列表2 列表3 有序列表 列表...

  • 学习小组Day1笔记-Herobrine

    Day1-Herobrine 列表 无序列表 无序列表 无序列表 无序列表 有序列表 有序列表 有序列表 有序列表...

  • H5学习从0到1-H5列表(8)

    列表的基本语法 ol:有序列表 ul:无序列表 li:列表项 dl:列表 dt:列表项 dd:列表描述 常用列表 ...

  • 第一篇简书

    MarkDown首次使用 无序列表 列表1 列表2 列表3 有序列表 有序列表1 有序列表2 有序列表3 有序列表...

  • 标题

    列表1 列表2 子列表1 子列表2 子列表3子子列表1子子列表2子子子列表1子子子列表2

  • markdown test

    header2 test> test 列表* 列表2 * 列表2.1 列表列表2列表2.1

网友评论

      本文标题:列表

      本文链接:https://www.haomeiwen.com/subject/rwewyftx.html