Data structure of JS (node) Implementation list

Source: Internet
Author: User
Tags prev

* Lists are used to hold data structures that are small in volume
* It is also convenient to use lists when there is a large amount of data that does not need to be looked up or sorted.

This data structure runs in node environment, need to have a basic understanding of node.

1. Listsize: list length

2. Pos Current Location

3. GetLength get the length of the list

4. ToString returns a string for the list

5.getElement gets the element of the current position

6. Insert is inserted after the specified position

7. Append insert at the end of the list

8. Remove delete Element

9.front move the current position to the first place

Ten end moves the current position to the end

Prev Move the current position forward one

Next move the current position back one

Currentpos Get the current location

MoveTo move the current position to the specified position

Find the specified element

The ergodic of the Loop list (index is indexed, Loopitem is the corresponding element)

/** @Author: wyt* @Date: 2018-06-14 17:11:19* @Last Modified by:wyt* @Last Modified time:2018-06-19 19:14:03*///lists are used to hold data structures that are small in volume//It is also convenient to use lists when there is a large amount of data that you do not need to find or sort. functionList () { This. listsize = 0;//List Length     This. pos = 0;//Current Position     This. clear = clear;//Clear List     This. getlength = GetLength;//get the length of a list     This. toString = tostring;//returns the string form of the list     This. getelement = getelement;//returns the element at the current position     This. Insert = insert;//inserts an element after the specified position     This. append =append;//add an element at the end of the list     This. remove = remove;//Remove the current element from the list     This. Front = Front;//move the current position of a list to the first element position     This. end =end;//move the current position of the list to the last element position     This. prev = prev;//moves the current position of the list forward by one     This. next = Next;//moves the current position of the list backward by one     This. Currentpos = Currentpos;//returns the current position of the list     This. moveTo = MoveTo;//moves the current position to the specified position     This. find = Find;//gets the position of the specified element     This. loop = loop;//List of the Walker     This. List = []; functionClear () { This. list.length = 0;  This. listsize = 0;  This. pos = 0; }    functionGetLength () {return  This. List.length; }    functiontoString () {return  This. List; }    functiongetelement () {return  This. list[ This. Pos]; }    functionInsert (item, after) {varindex = This. Find (after)if(index) { This. List.splice (index+1, 0, item);  This. listsize++; return1; }        return-1    }    functionAppend (item) { This. List.push (item);  This. listsize++; }    functionRemove (ele) { for(vari=0;i< This. list.length;i++) {            if( This. list[i]===ele) {                 This. List.splice (I, 1); return1; }Else{                return-1; }        }         This. listsize--; }    functionFront () { This. pos = 0; }    functionEnd () { This. pos = This. list.length-1; }    functionprev () { This. pos--; }    functionNext () { This. pos++; }    functionCurrentpos () {return  This. Pos; }    functionMoveTo (index) { This. pos =index; }    functionFind (ele) { for(vari = 0; i< This. list.length; i++) {            if( This. list[i] = =ele) {                returni}}return-1; }    functionLoop (CB) { for( This. Front (); This. Currentpos () < This. list.length; This. Next ()) {            varLoopitem = This. GetElement (); varindex = 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=NewList (); 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); })})

Data structure of JS (node) Implementation list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.