Home  >  Article  >  Web Front-end  >  Detailed explanation of the list of Javascript data structures and algorithms_javascript skills

Detailed explanation of the list of Javascript data structures and algorithms_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:10:011042browse

Foreword: In daily life, people often use lists. For example, when we sometimes go shopping, in order to buy all the things when shopping, we can make a list of the things we want to buy before going. This is the list we use. Or when we were in school when we were young, after each exam, the school would list the rankings and transcripts of the top ten students who scored in the exam, and so on. These are all examples of the list. We also use lists in our computers, so where are lists suitable for use? Where is it not suitable for use?

Suitable for use: When there are not many elements in the list, you can use the list, because the efficiency is very high when searching or sorting the elements in the list. On the contrary: If there are very many elements in the list, then Lists are no longer suitable.

1: Abstract data type definition of list

In order to design the abstract data type of the list, we need to give the definition of the list, including what attributes the list should have, what operations should be performed on the list, etc.

A list is an ordered set of data. The data items in each list are called elements. In JavaScript, elements in a list can be of any data type. There is no prior agreement on how many elements can be stored in a list. However, the number of elements is limited by the program memory in actual use.

Now we want to design a list, then we can think about implementing a list and what attributes and methods they should contain. Of course, my following designs are based on the demo in the "Javascript Data Structure and Algorithm" book. So far we can learn how to design our own abstract class as a reference when we write programs in the future. The most important thing for us to study the demos in the book now is to learn their design ideas and how 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 (attribute): The current position of the list, the index of the element.
3. dataStore (attribute): Initialize an empty array to save the number of elements. If we want to get a specific element in the list, we can use the pos attribute above; such as dataStore[pos];

All methods; explained in the following list, not introduced one by one.

2: How to implement the list class

According to the list abstract data type defined above, we can implement the following List class through the constructor prototype mode as follows.

Copy code The code is as follows:

function List() {
// Number of elements in the list
This.listSize = 0;

// What is the current position of the list
This.pos = 0;

// Initialize an empty array to save list elements
This.dataStore = [];

}

List.prototype = {
 
//Add element to the end of the list
append: function(element) {
      var self = this;
           self.dataStore[this.listSize] = element;
},

// Remove elements from the list
Remove: function(element) {
      var self = this;
      var curIndex = self.find(element);
If(curIndex > -1) {
                 self.dataStore.splice(curIndex,1);
​​​​​​—self.listSize;
             return true;
}
         return false;
},

// Find the element in the list and return the index
Find: function(element) {
      var self = this;
for(var i = 0,dataLen = self.dataStore.length; i < dataLen; i ) {
If(self.dataStore[i] == element) {
                   return i;
            }
}
         return -1;
},
 
// Return the number of elements in the list
length: function() {
          return this.listSize;
},

// Display elements in the list
toString: function(){
          return this.dataStore;
},

/*
* Insert an element after the specified element
* @param element current element
* @param elementAfter Insert the current element after this element
*/
​ insert: function(element,elementAfter){
      var self = this;
        var insertPos = self.find(elementAfter);
If(insertPos > -1) {
                 self.dataStore.splice(insertPos 1,0,element);
                self.listSize;
             return true;
}
         return false;
},
 
// Clear all elements in the list
Clear: function() {
          delete this.dataStore;
This.dataStore = [];
This.listSize = this.pos = 0;
},
// Determine whether the given element is in the list
contains: function(element) {
      var self = this;
for(var i = 0,ilen = self.dataStore.length; i < ilen; i ) {
If(self.dataStore[i] == element) {
                   return true;
            }
}
         return false;
},
// Move the current element in the list to the first position
Front: function(){
This.pos = 0;
},
// Move the current element in the list to the last position
End: function(){
This.pos = this.listSize - 1;
},
// Move the current position back one position
Prev: function(){
If(this.pos > 0) {
​​​​​​—this.pos;
}
},
// Move the current position forward one position
Next: function(){
If(this.pos < this.listSize - 1) {
This.pos;
}
},
// Return the current position of the list
CurPos: function(){
         return this.pos;
},
//Move the current position to the specified position
MoveTo: function(n) {
This.pos = n;
},
// Return the element at the current position
GetElement:function(){
          return this.dataStore[this.pos];
}
};

As above: Implement a list class, including as many methods as above. Of course, we can also extend some other methods to enrich the implementation of the list class. The most important thing is to learn the above coding method.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn