silver-hands / sss

0 stars 0 forks source link

【Q088】😈第六部分:栈与队列😈面试题09. 用两个栈实现队列 #88

Open fly0o0 opened 4 years ago

fly0o0 commented 4 years ago

面试题09. 用两个栈实现队列

fly0o0 commented 4 years ago
var CQueue = function() {
  this.stack1 = []
  this.stack2 = []
};

/**
 * @param {number} value
 * @return {void}
 */
CQueue.prototype.appendTail = function(value) {
  this.stack1.push(value)
};

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function() {
  if (this.stack2.length) return this.stack2.pop()
  if (this.stack1.length == 0) return -1
  while (this.stack1.length) {
    this.stack2.push(this.stack1.pop())
  }
  return this.stack2.pop()
};

/**
 * Your CQueue object will be instantiated and called as such:
 * var obj = new CQueue()
 * obj.appendTail(value)
 * var param_2 = obj.deleteHead()
 */