Sequence table of JavaScript data structures

Source: Internet
Author: User

About the concept of linear table here is not to repeat, you can self-Baidu and access to data, linear table in accordance with the storage (physical) structure into sequential storage and chain storage, each storage mode of the different decision of its implementation code is different:

Sequential storage is characterized by a contiguous address space in memory, which is then placed in a linear table, which facilitates the access of linear tables, but is not conducive to insertion and deletion, and can cause memory waste or overflow if the linear table length cannot be determined beforehand.

In this article I use JavaScript to implement the sequential table in the linear table.

The following code:

1 varOrderList =function(){2   varitems = [];//The linear table internally defines a 10-size array to store the data3    This. items = items;//This takes advantage of the fact that an array is a reference type, and instance properties and local variables are actually points to the same group.4 5    This. Findelem =function(data) {//find the element in the table, return the corresponding address location6     varSymbol =false;7     vartemp = 0;8      for(vari = 0;i < items.length;i++){9       if(Items[i] = =data) {TenSymbol =true; Onetemp = i+1; A       } -     } -     if(Symbol) { the         returntemp; -     } -   }; -  +    This. Getelem =function(num) {//Gets the element operation, returns the obtained element -     if(num > Items.length | | num < 1 | | items.length = = 0){ +       return false; A}Else{ at       returnITEMS[NUM-1];//Note that the array subscript starts at 0. -     } -   }; -  -    This. Listinsert =function(Data,pos) {//Inserts an element operation, returns a new array -     if(Pos > Items.length | | pos < 1 | | items.length = = 0){ in       return false; -}Else{ to        for(vari = items.length;i >= pos-1;i-= 1){ +ITEMS[I+1] =Items[i]; -       } theITEMS[POS-1] =data; *       returnitems; $     }Panax Notoginseng   }; -  the    This. Listdelete =function(POS) {//Delete element action, return deleted element +     vartemp = items[pos-1]; A     if(POS < 1 | | pos > Items.length | | items.length = = 0){ the       return false; +}Else{ -        for(vari = Pos;i < items.length;i++){ $ITEMS[I-1] =Items[i]; $       } -       returntemp; -     } the   }; - Wuyi  the }; -  Wu //instantiate the test for a moment - varList =Neworderlist (); AboutList.items.push ("A", "B", "C", "D", "E", "F", "G"); $ Console.log (list.items); -Console.log (List.findelem ("C")); -Console.log (List.getelem (1)); -Console.log (list. Listinsert ("J", 3)); AConsole.log (list. Listdelete (2)); +Console.log (List.items);

Here is the result of debugging in Chrome's console, and the test is correct.

Sequence table of JavaScript data structures

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.