Array
Array creation:
var troop=New Array (6); // Create an array of length 6 var troop=New Array (2,3,5,6,4,7) ;
Array method:
varStr= "I love JavaScript";varSingle=str.split ("");//' I ', ', ' l ', ' O ',.....varMutipy=str.split ("");//' I ', ' love ', ' JavaScript 'vartroop=NewArray (2,5,6,8,9,4,1,2);varIndex=troop.indexof (2);//index=0varnames=[' Jack ', ' Mike ', ' Molly '];varStr=names.join ();//str= ' jack,mike,molly 'varnums=[];nums.push (3,4); Nums.shift ();//nums=[1,2,3,4];Nums.pop ();//nums=[1,2,3];Nums.shift ();//nums=[2,3];Nums.splice (0,1,5,6)//nums=[5,6,3];
Operations on array elements (no new arrays are generated)varnums=[2,4,6,8];functionIsEven (num) {return num%2==0;}
function Square (num) {return num*num;}
Nums.foreach (square); Squared all elements of an array
var swit=nums.every (IsEven); Determines whether the array element is even, if it returns true;
To generate a new array:
Filter,map
Two-tuple array:
for (Var i=0;i<5;i++)
Troop[i]=[];
Stacks and lists are actually packages of array and array methods, so I omit not to write.
Linked list:
varlinklist=function(){ varNode=function(Element) { This. element=element; This. next=NULL; }; This. length=0; This. head=NewNode ("Head"); Defines the chain header, length, and, at the same time, defines four methods. This. append=append; This. insert=Insert; This. remove=remove; This. display=display; functionAppend (Element) {varNewnode=NewNode (Element); The list adds a new node at the end, noting that Node.next is still the node type, so you call the nodes constructor when you add a new nodevarThisnode= This. Head;
this.length++; if(thisnode.element==NULL)//When writing loop statements or judging statements, be aware that an empty node has no element elements, {//So first make sure that the thi Snode is a non-empty node, the If judgment statement can run normally, or it will error. Thisnode.element=element; }Else{ while(thisnode.next) {Thisnode=Thisnode.next; } Thisnode.next=NewNode; } }; functionInsert (elementbe,element) {varNodebe= This. Head; this.length++; varInsertnode=NewNode (Element); while(! (nodebe.element===elementbe)) {Nodebe=Nodebe.next; }; varNodeafter=Nodebe.next; Nodebe.next=Insertnode; Insertnode.next=Nodeafter; }; functionRemove (Element) {varNodedelete= This. Head; while(! (nodedelete.next.element===element)) {Nodedelete=Nodedelete.next; }; varNodeafter=nodedelete.next.next;//next can be superimposed multiple timesnodedelete.next=Nodeafter; }; functiondisplay () {varThisnode= This. Head; varStr= ""; while(thisnode) {str+=thisnode.element+ ""; Thisnode=Thisnode.next; } returnstr; } }; varnewlist=Newlinklist (); If the call constructor is not quite understood with new, you can refer to the previous post for a brief analysis of this newlist.append ("1"); Newlist.append ("2"); Newlist.append ("3"); Newlist.append ("4"); Newlist.insert ("3", "5") varStr=Newlist.display (); alert (str);
JavaScript data structures are continuously updated in: It'll be over in a week.