Appearance
剑指Offer 09. 用两个栈实现队列
js
var CQueue = function() {
// 入队栈
this.pushStack = []
// 出队栈
this.popStack = []
// 队列长度
this.size = 0
};
/**
* @param {number} value
* @return {void}
*/
CQueue.prototype.appendTail = function(value) {
// 入队栈入元素
this.pushStack.push(value)
this.size++
};
/**
* @return {number}
*/
CQueue.prototype.deleteHead = function() {
// 判空
if (this.size === 0) return -1
// 如果出队栈为空,那么就从入队栈中push进去
if (this.popStack.length === 0) {
while(this.pushStack.length > 0) {
this.popStack.push(this.pushStack.pop())
}
}
// 长度-1
this.size--
// 返回被删除的元素
return this.popStack.pop()
};
/**
* Your CQueue object will be instantiated and called as such:
* var obj = new CQueue()
* obj.appendTail(value)
* var param_2 = obj.deleteHead()
*/