js数据结构之列表的详细实现方法

* 列表用于存放数据量较少的数据结构
* 当数据量较大时,不需要对其进行查找、排序的情况下,使用列表也比较方便。

本数据结构在node环境下运行,需要对node有个基本是了解。

1. listSize:  列表长度

2. pos   当前位置

3. getLength  获取列表的长度

4. toString  返回列表的字符串

5.getElement  获取当前位置的元素

6. insert    在指定位置的后面插入

7. append   在列表的末尾插入

8. remove  删除元素

9.front  把当前位置移到首位

10 end  把当前位置移到末尾

11. prev 把当前位置前移一位

12. next 把当前位置后移一位

13. currentPos 获取当前位置

14  moveTo   把当前位置移动到指定位置

15  find  找到指定的元素

16 loop   列表的遍历器(index为索引,loopItem为对应的元素)

 下面通过fs模块将制定txt文档中的内容装进列表中:

列表对象数据如下:

function List () {
    this.listSize = 0;  // 列表长度
    this.pos = 0;  // 当前位置
    this.clear = clear; // 清空列表
    this.getLength = getLength; // 获取列表的长度
    this.toString = toString; // 返回列表的字符串形式
    this.getElement = getElement; // 返回当前位置的元素
    this.insert = insert; //在当前位置元素的后面插入元素
    this.append =append; // 在列表的末尾添加元素
    this.remove = remove; // 从列表中删除当前元素
    this.front = front; // 将列表的当前位置移动到第一个元素位置
    this.end =end; // 将列表的当前位置移动到最后一个元素位置
    this.prev = prev; // 将列表的当前位置向前移动一位
    this.next = next; // 将列表的当前位置向后移动一位
    this.currentPos = currentPos; // 返回列表的当前位置
    this.moveTo = moveTo; // 将当前位置移动到指定位置
    this.find = find; // 获取指定元素的位置
    this.loop = loop; // 列表的遍历器
    this.list = [];



    function clear () {
        this.list.length = 0;
        this.listSize = 0;
        this.pos = 0;
    }

    function getLength () {
        return this.list.length;
    }


    function toString () {
        return this.list;
    }

    function getElement () {
        return this.list[this.pos];
    }

    function insert (item, after) {
        var index = this.find(after)
        if(index){
            this.list.splice(index+1, 0, item);
            this.listSize++;
            return 1;
        }
        return -1
    }

    function append (item) {
        this.list.push(item);
        this.listSize++;
    }

    function remove (ele) {
        for (var i=0; i<this.list.length; i++) {
            console.log(this.list[i]);
            if(this.list[i]==ele){
                this.list.splice(i, 1);
                console.log("remove");
                return 1;
            }
        }

        console.log("not remove");
                
        this.listSize--;
        return -1;
        
    }

    function front () {
        this.pos = 0;
    }

    function end () {
        this.pos = this.list.length-1;
    }


    function prev () {
        this.pos--;
    }

    function next () {
        this.pos++;
    }

    function currentPos () {
        return this.pos;
    }

    function moveTo (index) {
        this.pos = index;
    }

    function find (ele) {
        for (var i = 0; i<this.list.length; i++) {
            if(this.list[i] == ele) {
                return i
            }
        }
        return -1;
    }

    function loop (cb) {
        for(this.front();this.currentPos()<this.list.length;this.next()){
            var loopItem = this.getElement();
            var index = this.currentPos();
            (function(index, loopItem){
                cb(index, loopItem);
            })(index, loopItem)
        }
    }
}

 

.txt文件如下:

(1) The Shawshank Redemption(《肖申克的救赎》)
(2) The Godfather(《教父》)
(3) The Godfather: Part II(《教父2》)
(4) Pulp Fiction(《低俗小说》)
(5) The Good, the Bad and the Ugly(《黄金三镖客》)
(6) 12 Angry Men(《十二怒汉》)
(7) Schindler’s List(《辛德勒名单》)
(8) The Dark Knight(《黑暗骑士》)
(9) The Lord of the Rings: The Return of the King(《指环王:王者归来》)
(10) Fight Club(《搏击俱乐部》)
(11) Star Wars: Episode V - The Empire Strikes Back(《星球大战5:帝国反击战》)
(12) One Flew Over the Cuckoo’s Nest(《飞越疯人院》)
(13) The Lord of the Rings: The Fellowship of the Ring(《指环王:护戒使者》)
(14) Inception(《盗梦空间》)
(15) Goodfellas(《好家伙》)
(16) Star Wars(《星球大战》)
(17) Seven Samurai(《七武士》)
(18) The Matrix(《黑客帝国》)
(19) Forrest Gump(《阿甘正传》)
(20) City of God(《上帝之城》)

利用fs读取后将每一项装入列表中:

const fs = require("fs");

var listData = fs.readFileSync("movie.txt", "utf-8").split("\r\n");

listData.forEach(function(item){
    movieList.append(item);
})

 

转载于:https://www.cnblogs.com/pomelott/p/9200761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值