JavaScript data structures and algorithms reading notes > Chapter III list

Source: Internet
Author: User

1. Structural Analysis

The list must first have the following properties:

Listsize length

POS Current Location

DataStore data

We will work with the above three properties in the following ways:

Length () Get Lengths |   GetPos () get the current position | ToString () Print List contents

Add (ele) Order elements |  Insert (Newele, Afterele) inserts an element after an element | Remove (ele) deletes an element

Clear () empty list |  Find (ele) query element Location |  Get (index) gets elements based on location | Contain (ele) contains an element

Prev () subscript move forward One |   Next () Subscript move One |   Front () position to head | End () position to trailer

2. Achieve:

functionList () { This. listsize = 0;  This. DataStore = [];  This. pos = 0;  This. length =function(){         return  This. listsize;    };  This. GetPos =function(){        return  This. Pos;    };  This. prev =function(){         This. pos--;    };  This. Next =function(){         This. pos++;    };  This. Front =function(){         This. pos = 0; return0;    };  This. end =function(){         This. pos = This. listsize; return  This. listsize;    };  This. Find =function(ele) { for(vari=0; i< This. listsize; i++){            if( This. datastore[i] = = =ele) {                returni; }        }        return-1;    };  This. add=function(ele) { This. datastore[ This. listsize++] =Ele;    };  This. Insert =function(Ele, Afterele) {varindex = This. Find (Afterele); if(Index >-1){             This. Datastore.splice (Index, 0, ele);  This. listsize++; return true; }Else{            return false;    }    };  This. remove =function(ele) {varindex = This. Find (Ele); if(Index >-1){             This. Datastore.splice (Index, 1);  This. listsize--; return true; }        return false;    };  This. Clear =function(){        Delete  This. DataStore;  This. DataStore = [];  This. Listsize = This. pos = 0;    };  This. contains =function(ele) {if( This. Find (Ele) >-1){            return true; }Else{            return false;    }    };  This. get =function(index) {return  This. Datastore[index];    };  This. toString =function(){        return  This. datastore.tostring (); };}

3. Application:

var users = [' Jenny ', ' Penny ', ' tenny ', ' Anny ', ' Lily ']; var New List ();  for (var i = 0; I <users.length-1; i++) {    list.add (users[i]);}  for (List.front (); List.getpos () <list.length (); List.next ()) {    + ":" + List.get (List.curpos ()));}

  

JavaScript data structures and algorithms reading notes > Chapter III list

Related Article

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.