JavaScript fun: ordered list

Source: Internet
Author: User
Unlike an unordered list, an ordered list stores ordered elements, such as ascending or descending. In the implementation of many ordered lists, you certainly cannot find operations similar to addAfter, because it is one of the features of unordered lists. Unlike an unordered list, an ordered list stores ordered elements, such as ascending or descending. In the implementation of many ordered lists, you certainly cannot find operations similar to addAfter, because it is one of the features of unordered lists.

Generally, the ordered list provides several basic operations:

1. add is used to add elements to the list and maintain its orderly state.

2. get operation, used to obtain the elements under the specified index

3. Get the length of the list by using the length attribute or method.

In these operations, add is the most important part, which is usually composed of three parts (taking incremental sequence as an example ):

1. traverse the list from left to right until an element greater than or equal to the inserted value is found. The position of the element is the position to be inserted.

2. Move the element on the right of the insert position one by one

3. Insert the value to this position

Function SortedList () {this. length = 0; this. elementData = [];} SortedList. prototype. add = function (val) {var array = this. elementData; for (var I = 0; I = I; j --) {array [j + 1] = array [j];} array [I] = val; this. length ++;} SortedList. prototype. get = function (I) {return this. elementData [I];}

The above is the JavaScript interesting question: the content of the ordered list. For more information, see the PHP Chinese website (www.php1.cn )!

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.