---List of javascript data structures and algorithms
in daily life, people often want to use the list, for example, we have to go shopping at times, in order to buy everything when shopping, we can go before, the list of things to buy, this will be used in the lists, or when we were young school that period of time, after each test, The school will list the top 10 students of the test scores and transcripts, etc. We also use lists in our computers, so where is the list suitable for use? Not suitable for use in what place?
suitable for use in : When the list of elements is not a lot of cases, you can use the list, because the list of elements in the search or sorting, the efficiency is very high, and vice versa: If the list of elements in a very large number of cases, it is not appropriate to use the list.
One: Abstract data type definition for a list
To design an abstract data type for a list, we need to define the list, including what attributes the list should have, what actions should be performed on the list, and so on.
A list is an ordered set of data. The data items in each list are called elements. In JavaScript, the elements in a list can be any data type. How many elements can be saved in a list without prior agreement. However, the number of elements in actual use is limited by program memory.
Now that we want to design a list, we can think about implementing a list, what properties and methods they should include, and of course, the design below is based on the demo on the "JavaScript data structure and algorithms" book, so we can learn to do it later if we write the program How to design our own abstract class as a reference, we now learn the book on the demo is the most important to learn their design ideas and the way to write code. They have the following attributes;
1. Listsize (attribute): Use a listsize variable to save the number of elements in the list.
2. POS (properties): The current position of the list, the index of the element.
3. DataStore (attribute): Initializes an empty array to hold the number of elements. If we want to get a specific list of elements can use the above POS attribute; Datastore[pos];
All the methods are explained in the following list, not introduced.
listsize (attributes) |
Number of elements in the list |
POS (properties) |
The current position of the list is the first number |
DataStore (attributes) |
Initializes an empty array to hold the list elements |
Append (method) |
Add an element to the end of the list |
Remove (method) |
Remove an element from the list |
Find (method) |
Find an element in a list returns an index |
Length (method) |
Returns the number of elements in a list |
ToString (method) |
Display elements in a list |
Insert (method) |
Inserts an element after the specified element |
Clear (method) |
Empty all the elements in the list |
Contains (method) |
Determines whether the given element is in the list |
Front (method) |
Moves the current element in a list to the first position |
End (method) |
Moves the current element in the list to the last position |
Prev (method) |
Move the current position back one |
Next (method) |
Move the current position forward one |
CurPos (method) |
Returns the current position of the list |
MoveTo (method) |
Moves the current position to the specified position |
GetElement (method) |
Returns the element at the current position |
Second: How to implement the List class.
Based on the list of abstract data types defined above, we can implement a list class as follows by constructor + prototype mode.
functionList () {//number of elements in the list This. listsize = 0; //the current position of the list is the first number This. pos = 0; //Initializes an empty array to hold the list elements This. DataStore = [];} List.prototype= { //add an element to the end of the listAppendfunction(Element) {varSelf = This; self.datastore[ This. listsize++] =element; }, //remove an element from the listRemovefunction(Element) {varSelf = This; varCurindex =Self.find (Element); if(Curindex >-1) {Self.dataStore.splice (Curindex,1); --self.listsize; return true; } return false; }, //find an element in a list returns an indexFindfunction(Element) {varSelf = This; for(vari = 0,datalen = Self.dataStore.length; i < Datalen; i++) { if(Self.datastore[i] = =Element) { returni; } } return-1; }, //returns the number of elements in a listLengthfunction() { return This. listsize; }, //display elements in a listTostring:function(){ return This. DataStore; }, /** Insert an element after the specified element * @param element Current elements * @param elementafter inserts the current element behind this element*/Insert:function(element,elementafter) {varSelf = This; varInsertpos =Self.find (Elementafter); if(Insertpos >-1) {Self.dataStore.splice (Insertpos+1,0, Element); ++self.listsize; return true; } return false; }, //empty all the elements in the listClearfunction() { Delete This. DataStore; This. DataStore = []; This. Listsize = This. pos = 0; }, //determines whether the given element is in the listContainsfunction(Element) {varSelf = This; for(vari = 0,ilen = Self.dataStore.length; i < Ilen; i++) { if(Self.datastore[i] = =Element) { return true; } } return false; }, //moves the current element in a list to the first positionFrontfunction(){ This. pos = 0; }, //moves the current element in the list to the last positionEndfunction(){ This. pos = This. listSize-1; }, //move the current position back onePrevfunction(){ if( This. pos > 0) { -- This. Pos; } }, //move the current position forward oneNextfunction(){ if( This. POS < This. listSize-1) { ++ This. Pos; } }, //returns the current position of the listCurPos:function(){ return This. Pos; }, //moves the current position to the specified positionMoveTo:function(n) { This. pos =N; }, //returns the element at the current positionGetElement:function(){ return This. datastore[ This. Pos]; }};
As above: the implementation of a list of classes, including the above so many methods, of course, we can also extend some other methods to enrich the implementation of the list class, the most important to learn as the encoding method.
---List of javascript data structures and algorithms