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