A list of data structures-javascript implementation

Source: Internet
Author: User

Record of learning data structure

A list is a finite sequence of data items, that is, a set of data items arranged in a certain linear order, the basic operations on such a data structure include the lookup of elements, the insertion, and the deletion of the list of two main manifestations are arrays and linked lists, stacks and queues are two special types of list

Iterators (iterator), sometimes called cursors, are programming software design patterns that can be visited on a container (container, for example, a linked list or array), and designers do not need to care about the contents of the container

Front,end,prev,next uses the principle of iterators

' Use strict 'functionList () { This. Size = 0;  This. pos = 0;  This. data = [];  This. Clear =Clear;  This. Find =find;  This. toString =toString;  This. Insert =Insert;  This. Append =append;  This. remove =remove;  This. Front =Front;  This. end =end;  This. prev =prev;  This. Next =Next;  This. length =length;  This. Currpos =Currpos;  This. MoveTo =MoveTo;  This. getelement =getelement;  This. contains =contains;    }; functionAppend (Element) { This. data[ This. size++] =element; }    functionFind (Element) { for(vari = 0; I < This. data.length; i++){            if( This. data[i] = =Element) {                returni; }        }        return-1; }    functionRemove (Element) {varFoundat = This. Find (Element); if(Foundat >-1){             This. Data.splice (foundat,1); -- This. Size; return true; }        return false; }    functionLength () {return  This. Size; }    functiontoString () {return  This. Data; }    functionInsert (element, after) {varInsertpos = This. Find (after); if(Insertpos >-1){             This. Data.splice (insertpos+1, 0, Element); ++ This. Size; return true; }        return false; }    functionClear () {Delete  This. Data;  This. data.length = 0;  This. Size = This. pos = 0; }    /*determines whether the query element is within the list*/    functioncontains (element) { for(vari = 0; I < This. data.length; i++) {            if( This. data[i] = =Element) {                return true; }        }        return false    }    /*move to the first element*/    functionFront () { This. pos = 0; }    /*move to the last element*/    functionEnd () { This. pos = This. size-1; }    functionprev () {if( This. pos > 0){            -- This. Pos; }    }    functionNext () {if( This. POS < This. size-1){            ++ This. Pos; }    }    functionCurrpos () {return  This. Pos; }    functiongetelement () {return  This. data[ This. Pos]; }    varNames =NewList (); Names.append (' Lily '); Names.append (' Tom '); Names.append (' King '); Names.append (' Lihua '); Names.append (' Lisi ');    Names.front (); Console.log (Names.getelement ())//LilyNames.next (); Console.log (Names.getelement ())//Tom    varASD = Names.find (' King ') Console.log (ASD)//2Names.insert (' Jacks ', ' King ') Console.log (names.data)//["Lily", "Tom", "King", "Jacks", "Lihua", "Lisi")

A list of data structures-javascript implementation

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.