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