/*Queue FIFO*/ (function () { varQueue = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K '], Ihead= 0, Itail =queue.length, result= []; while(Ihead!==itail) {Result.push (queue[ihead]); Queue[ihead]=NULL; Ihead++; //if (ihead = = = Itail) { //Break ; //}Queue[itail] =(Queue[ihead]); Ihead++; Itail++; } console.log (Result.join (", ")); }());
/*Stack LIFO*/ (function () { varStack1 = [], Stack2 = [], Top1 =-1, TOP2 = 1, Hash= {"{": "}", "[": "]", "(": ")"}, result =[], VL, VR; Stack1= ["{", "[", "(", ")", "(", ")", "]", "[", "(", ")", "]", "(", ")", "}"]; Top1= Stack1.length-1; while(TOP1 >= 0) {VL= stack1[top1--]; Switch(VL) { Case"{": Case"[": Case"(": VR= Top2 >= 0? stack2[top2--]: "Undefined"; if(HASH[VL]!==VR) {Console.log ("Error:" + VL + "!==" +VR); return; } Else{Result.push ([VL, VR]); } Break; Case"}": Case"]": Case")": stack2[++TOP2] =VL; Break; } } if(TOP2 >= 0) {Console.log ("Error:don ' t find out the same sign:" + stack2.slice (0, TOP2 + 1). Join (",")); } Else{console.log (Result.join (", ")); } }());
/*chain Table Array Simulation list: Two arrays, an array with corresponding values, an array to be stored relative to the index at the next worthy index, that is, the index table*/ (function () { varNode =function(value, nextnode) { This. Value =value; This. NextNode =NextNode; }; varChain =NewNode (1,NewNode (3,NewNode (5,NewNode (7,NewNode (9,NULL))) , Fninsert=function(value, node) {if(!node.nextnode | | node.nextNode.value >=value) {Node.nextnode=NewNode (value, Node.nextnode); return; } Else{Fninsert (value, Node.nextnode); }}, Fnprint=function(node) {return[Node.value].concat (!! Node.nextnode?Fnprint (Node.nextnode): []); }; //InsertConsole.log (Fnprint (chain)); varInsertvalue = 6; Fninsert (6, chain); Console.log (Fnprint (chain)); }());
Daily english-2015/02/02– algorithm (structure)