JavaScript資料結構 持續更新中。。一周后可完結

來源:互聯網
上載者:User

標籤:列表   square   tip   運行   建立   for   eve   return   single   

array:

數組建立:

var troop=new Array(6);                    //建立一個長度為6的數組var troop=new Array(2,3,5,6,4,7);

數組方法:

var str="I love javascript";var single=str.split("");           //‘I‘,‘ ‘,‘l‘,‘o‘,.....var mutipy=str.split(" ");        //‘I‘,‘love‘,‘javascript‘var troop=new Array(2,5,6,8,9,4,1,2);var index=troop.indexOf(2);    //index=0var names=[‘jack‘,‘mike‘,‘molly‘];var str=names.join();               //str=‘jack,mike,molly‘var nums=[];nums.push(3,4);nums.shift(1,2);                      //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];
對數組元素進行操作(不產生新數組)var nums=[2,4,6,8];function isEven(num){return num%2==0;}
function square(num){return num*num;}
nums.forEach(square); //對數組所有元素平方
var swit=nums.every(isEven); //判斷數組元素是否均為偶數,若是,返回true;

產生新數組:
filter,map

二元數組:
for(var i=0;i<5;i++)
troop[i]=[];

棧和列表其實就是對數組和數組方法的封裝,所以我省略不寫。 

鏈表:

    var linkLIst=function(){        var Node=function(element){            this.element=element;            this.next=null;        };        this.length=0;                this.head=new Node("head");                 //定義鏈表頭,長度,同時,定義了四個方法。        this.append=append;        this.insert=insert;                this.remove=remove;        this.display=display;                function append(element){            var newNode=new Node(element);          //鏈表在末尾添加新節點,注意的是,node.next仍是node類型,故在添加新節點時要調用節點建構函式            var thisNode=this.head;
this.length++;
if(thisNode.element==null) //在寫迴圈語句或者判斷語句的時候要注意,一個空節點是沒有element元素的, { //因此要先確保thisNode為非空節點,該if判斷語句才能正常運行,不然會報錯。 thisNode.element=element; }else{ while(thisNode.next) { thisNode=thisNode.next; } thisNode.next=newNode; } }; function insert(elementbe,element){ var nodebe=this.head; this.length++; var insertNode=new Node(element); while(!(nodebe.element===elementbe)){ nodebe=nodebe.next; }; var nodeafter=nodebe.next; nodebe.next=insertNode; insertNode.next=nodeafter; }; function remove(element){ var nodeDelete=this.head; while(!(nodeDelete.next.element===element)) { nodeDelete=nodeDelete.next; }; var nodeAfter=nodeDelete.next.next; //next可疊加多次 nodeDelete.next=nodeAfter; }; function display(){ var thisNode=this.head; var str=""; while(thisNode){ str+=thisNode.element+" "; thisNode=thisNode.next; } return str; } }; var newlist=new linkLIst(); //如果調用建構函式為什麼用new不太理解,可以參考上一篇博文我對this的簡析 newlist.append("1");newlist.append("2");newlist.append("3");newlist.append("4");newlist.insert("3","5") var str=newlist.display(); alert(str);

 

JavaScript資料結構 持續更新中。。一周后可完結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.