javascript資料結構-棧

來源:互聯網
上載者:User

標籤:splay   增刪改查   堆棧   targe   gen   cte   close   size   string   

github部落格地址

棧(stack)又名堆棧,它是一種運算受限的線性表。遵循後進先出原則,像垃圾桶似的。
功能實現依然按照增刪改查來進行,內部資料存放區可以借用語言原生支援的數組。

棧類
function Stack(){this.data = [];}

  

添加資料

資料添加到末尾

push: function (element){this.data.push(element);}

  

刪除資料

從末尾刪除

pop: function (){this.data.pop();}

  

擷取資料

返回最後一個添加的

peek: function (){return this.data[this.size() - 1];}

  

是否為空白
isEmpty: function (){return this.data.length == 0;}

  

清空資料
clear: function (){this.data= [];}

  

資料長度
size: function (){return this.data.length;}

  

輔助函數,列印資料
print: function (){console.log(this.data.toString());}

  

完整代碼
 1 function Stack(){ 2     this.data = []; 3 } 4 Stack.prototype = { 5     push: function (element){ 6         this.data.push(element); 7     }, 8     pop: function (){ 9         this.data.pop();10     },11     peek: function (){12         return this.data[this.data.length - 1];13     },14     isEmpty: function (){15         return this.data.length == 0;16     },17     clear: function (){18         this.data= [];19     },20     size: function (){21         return this.data.length;22     },23     print: function (){24         console.log(this.data.toString());25     }26 }
View Code

 

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.