List of JavaScript data structure lists

Source: Internet
Author: User
Tags prev

I. Summary of Prerequisites

In the normal development of JavaScript programs, the display of the list data is often used. For example: Heat list, headline list, achievement list, to-do items, and so on, the above data have some common characteristics, the data structure is simple, the amount of data is small. Then, based on the above characteristics, to customize a list class, only need to encapsulate a few queries, positioning and other methods can help us to deal with the data. Of course, once the data structure is very complex, and the amount is larger, the role of the list class is not that big.

Second, data abstraction

1. Attribute definitions:

Property Note
DataSource stored data sources for lists
Size The length and number of the list
Position The current index position of the list

  

2. Method definition:

Method Note
Append adding elements
Insert Insert element (Specify position insertion)
Remove Delete Element
Clear Clear List
Find Querying elements
Get Get element
First Gets the first element
Last Gets the last element
Prev Move to previous position
Next Move to the next position
Front Move to start position
End Move to end position
Hasprev Whether there is a previous element
Hasnext Whether there is a later element
MoveTo Move to the specified position
Length Returns the length of the list
Contains Whether to include the specified element
Tostring Return Data source DataSource

The above data structure is defined according to the actual requirement, and the different usage environment can be modified accordingly.

Third, the code implementation
1 //List Class2 varList =function () {3     //Data Source4      This. DataSource = [];5     //Data Length6      This. Size = 0;7     //Current Position8      This. Position = 0;9 };Ten  OneList.prototype.constructor =List; A  -List.prototype = { -     //adding elements theAppendfunction(Element) { -          This. datasource[ This. size++] =element; -     }, -     //inserting elements +Insertfunction(position, Element) { -         if(Position >-1) { +              This. Datasource.splice (position + 1, 0, Element); A              This. size++; at             return true; -         } -         return false; -     }, -     //remove Element -Removefunction(position) { in          This. Datasource.splice (Position, 1); -     }, to     //Clear Data Source +Clearfunction () { -         Delete  This. DataSource; the          This. datasource.length = 0; *          This. Size = 0; $          This. Position = 0;Panax Notoginseng     }, -     //Querying Data theFindfunction(Element) { +          for(vari = 0; I < This. datasource.length; i++) { A             if( This. datasource[i] = =Element) { the                 returni; +             } -         } $         return-1; $     }, -     //Get Data -Getfunction () { the         return  This. datasource[ This. position]; -     },Wuyi     //get the first piece of data theFirstfunction () { -         return  This. datasource[0]; Wu     }, -     //get the last piece of data AboutLastfunction () { $         return  This. datasource[ This. size-1]; -     }, -     //move to previous position -Prevfunction () { A          This. position--; +     }, the     //move to the next position -Nextfunction () { $          This. position++; the     }, the     //move to start position theFrontfunction () { the          This. Position = 0; -     }, in     //move to end position theEndfunction () { the          This. Position = This. size-1; About     }, the     //whether there is a previous element theHasprev:function () { the         return  This. position > 0; +     }, -     //whether there is a later element theHasnext:function () {Bayi         return  This. Position < This. size-1; the     }, the     //move to the specified position -MoveTo:function(position) { -          This. Position =position; the     }, the     //Data Source Length theLengthfunction () { the         return  This. Size; -     }, the     //whether to include the specified element theContainsfunction(Element) { the          for(vari = 0; I < This. datasource.length; i++) {94             if( This. datasource[i] = =Element) { the                 return true; the             } the         }98         return false; About     }, -Tostring:function () {101         return  This. DataSource;102     }103};
Iv. Demonstration of examples
1 varList =NewList ();2List.append ("Zhang San");3List.append ("John Doe");4List.append ("Harry");5List.append ("Zhao Liu");6Console.log ("list dataSource, size, Position:", List.datasource, List.size, list.position);7 //Print: List dataSource, size, Position: ["Zhang San", "John Doe", "Harry", "Zhao Liu"] 4 08 9Console.log ("List Get ():", List.get ());Ten //Print: List get (): Zhang San One  A List.next (); -Console.log ("List prev ():", List.get ()); - //Printed: List prev (): John Doe the  -List.moveto (3); -Console.log ("List moveTo ():", List.get ()); - //Printed: List MoveTo (): Zhao Liu +  - varPosition = List.find ("Harry"); +Console.log ("List find ():", position); A //Printing: List Find (): 2 at  - varresult = List.contains ("Cao Qi"); -Console.log ("list contains ():", result); - //Printed: List contains (): false

List of JavaScript data structure lists

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.