博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js数据结构之列表的详细实现方法
阅读量:4681 次
发布时间:2019-06-09

本文共 2686 字,大约阅读时间需要 8 分钟。

* 列表用于存放数据量较少的数据结构

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

本数据结构在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

 

.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

你可能感兴趣的文章
《大道至简》3
查看>>
黑马程序员——关于接口和抽象类
查看>>
【译】为迁移到Windows Azure制定规划
查看>>
面试题之求二叉树的深度
查看>>
struts2---访问WEB
查看>>
jQuery学习(三)
查看>>
[VJ]输出m/n,若是循环体只输出第一节
查看>>
【问题】background:url(imagePath)不能显示图片
查看>>
linux 安装mysql
查看>>
团队项目冲刺第五天
查看>>
Dubbo-Fail to decode request due to: RpcInvocation
查看>>
android笔记5——同一个Activity中Fragment的切换
查看>>
Pillow & OpenCV安装
查看>>
【3dsmax2016】安装图文教程、破解注册以及切换语言方法
查看>>
markdown
查看>>
WebService-01-使用jdk发布第一个WebService服务并调用
查看>>
mysql 关键字于数据库字段于关键字冲突的问题
查看>>
【bzoj2694】Lcm 莫比乌斯反演+线性筛
查看>>
【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改
查看>>
Django表查询补充
查看>>