JavaScript data structure and algorithm list detailed _javascript tips

Source: Internet
Author: User
Tags abstract

In the daily life, people often want to use lists, such as we sometimes go shopping, in order to buy everything when shopping, we can before going to the list of things to buy, this will be used in lists, or when we were young school that time, each test after the test, The school will list the top 10 students ranked and transcripts, and so these are the list of the case. Our computer is also using a list, so where is the list suitable for use? Not suitable to use in what place?

Suitable for: When the elements of a list are not many, you can use a list because it is very efficient to find or sort the elements in a list, whereas it is not appropriate to use a list if the list element is very large.

One: Abstract data type definition for lists

To design the abstract data type of the 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 a set of ordered 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 the 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 of what attributes and methods they should contain, and of course the following design is based on the demo in the JavaScript data structure and algorithms book, so we can learn that if we write the program later, How to design our own abstract class as a reference, we now learn the book on the demo is mainly to learn their design ideas and the way to write code. They have the following attributes;

1. Listsize (properties): Use a listsize variable to hold the number of elements in the list.
2. POS (properties): The current position of the list, the index of the element.
3. Datastore (properties): Initializes an empty array to hold the number of elements. If we want to get the elements of a specific list, we can use the above POS attribute, such as Datastore[pos];

All the methods, as explained in the following list, are not introduced.

Two: How to implement the list class

Based on the abstract data type defined above, we can implement a list class as follows through the constructor + prototype pattern.

Copy Code code as follows:

function List () {
Number of elements in a list
this.listsize = 0;

The current position of the list is the first few
This.pos = 0;

Initializes an empty array to hold the element of the list
This.datastore = [];

}

List.prototype = {

Add an element to the end of the list
Append:function (Element) {
var self = this;
self.datastore[this.listsize++] = element;
},

   //Remove elements from 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;
   },

   //Lookup list elements return index
    find:function (Element) {
    & nbsp;   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
   },

Show elements in a list
Tostring:function () {
return this.datastore;
},

/*
* Inserts an element after the specified element
* @param element's current elements
* @param elementafter Inserts the current element behind 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;
},

Empty all the elements in the list
Clear:function () {
Delete This.datastore;
This.datastore = [];
this.listsize = This.pos = 0;
},
Determines whether a 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;
},
Moves the current element in the list to the first position
Front:function () {
This.pos = 0;
},
Moves the current element in the list to the last location
End:function () {
This.pos = this.listsize-1;
},
Move the current position back one
Prev:function () {
if (This.pos > 0) {
--this.pos;
}
},
Move the current position forward one
Next:function () {
if (This.pos < this.listsize-1) {
++this.pos;
}
},
Returns the current position of the list
Curpos:function () {
return this.pos;
},
Moves the current position to the specified location
Moveto:function (n) {
This.pos = n;
},
Returns the element of the current position
Getelement:function () {
return This.datastore[this.pos];
}
};

As above: implementation of a list class, including the above above so many methods, of course, we can also expand some other methods to enrich the implementation of the list class, the most important to learn such as the encoding method.

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.