var CQueue = function () {
this.stackA = [];
this.stackB = [];
};
CQueue.prototype.appendTail = function (value) {
this.stackA.push(value);//永远从A入队
};
CQueue.prototype.deleteHead = function () {
if (!this.stackA.length && !this.stackB.length) return -1;//如果两个栈都没有元素了,返回-1
if (!this.stackB.length) {
while (this.stackA.length) {
this.stackB.push(this.stackA.pop());//依次从A栈中将数据push进B栈
}
}
return this.stackB.pop();//从B出队
};
思路:进入队列都是从A栈进入,出队列都是从B栈出,如果B栈中没有数据了,就依次从A栈中将数据push进B栈,如果两个栈都没有元素了,返回-1