使用隊列實現棧的下列操做:javascript
注意:java
javascript中沒有隊列這種數據結構,所以用數組來實現
var MyStack = function() { this.stack = []; //初始化空數組 this.top = 0; //記錄棧頂的位置 } Mystack.prototype.push = function(x) { this.stack.push(x); this.top++; } Mystack.prototype.pop = function(x) { return this.stack[--this.top]; //返回棧頂元素並將top值減一 } MyStack.prototype.empty = function() { return !this.len };