码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-06-19 20:10:43      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:\n   ext   cti   current   nod   ++   index   mod   pomelo   

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

本数据结构在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)实现列表的数据结构

标签:\n   ext   cti   current   nod   ++   index   mod   pomelo   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!