分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

js(node)实现列表的数据结构

发布时间:2023-09-06 02:00责任编辑:熊小新关键词: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为对应的元素)

/** @Author: wyt* @Date: ??2018-06-14 17:11:19* @Last Modified by: ??wyt* @Last Modified time: 2018-06-19 19:14:03*/// 列表用于存放数据量较少的数据结构// 当数据量较大时,不需要对其进行查找、排序的情况下,使用列表也比较方便。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++) { ???????????if(this.list[i]===ele){ ???????????????this.list.splice(i, 1); ???????????????return 1; ???????????}else{ ???????????????return -1; ???????????} ???????} ???????this.listSize--; ???????????} ???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) ???????} ???}}// var li = new List();// li.append("kevin");// li.append("tate");// li.append("pomelott");// li.loop(function(index, loopItem){// ????console.log(index, loopItem);// })const fs = require("fs");let movieListData;let movieList = new List();fs.readFile("movie.txt", "utf-8", function(err, data){ ???movieListData = data.split("\r\n"); ???movieListData.forEach(function(item){ ???????movieList.append(item); ???}) ???movieList.loop(function(index, loopItem){ ???????console.log(index, loopItem); ???})})

js(node)实现列表的数据结构

原文地址:https://www.cnblogs.com/pomelott/p/9200761.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved