0
点赞
收藏
分享

微信扫一扫

队列实现栈,栈实现队列,你学废了吗?


队列与栈

相关概念

  • 队列是先进先出,栈是先进后出。
  • 以栈为例,如图所示。
  • 队列实现栈,栈实现队列,你学废了吗?_javascript

例题1:用栈实现队列

题目链接:[232.用栈实现队列](​​232. 用栈实现队列 - 力扣(LeetCode) (leetcode-cn.com)​​)

队列实现栈,栈实现队列,你学废了吗?_先进先出_02


队列实现栈,栈实现队列,你学废了吗?_入栈_03

分析:

  • 需要使用两个栈实现,一个为输入栈,一个为弹出栈
  • 由于队列的输出顺序刚好是和栈相反的,所以当输出栈为空时,就讲输入栈里面弹出,放入输出栈,这个时候输出栈的输出顺序就和队列的输出顺序一致。

MyQueue.prototype.pop = function() {
if(this.stackOut.length === 0) {
while(this.stackIn.length > 0){
this.stackOut.push(this.stackIn.pop());
}
}
return this.stackOut.pop();
};

  • 当需要获取最上面的元素(队列首部元素)时,其实也就是输出栈输出的第一个元素,可将其输出获取到值以后再将其压入栈。

MyQueue.prototype.peek = function() {
const x = this.pop();
this.stackOut.push(x);
return x;
};

解答

var MyQueue = function() {
this.stackIn = [];
this.stackOut = [];
};

MyQueue.prototype.push = function(x) {
this.stackIn.push(x);
};

MyQueue.prototype.pop = function() {
if(this.stackOut.length === 0) {
while(this.stackIn.length > 0){
this.stackOut.push(this.stackIn.pop());
}
}
return this.stackOut.pop();
};

MyQueue.prototype.peek = function() {
const x = this.pop();
this.stackOut.push(x);
return x;
};

MyQueue.prototype.empty = function() {
return !this.stackIn.length && !this.stackOut.length;
};

例题2:用队列实现栈

题目链接:[225.用队列实现栈](​​225. 用队列实现栈 - 力扣(LeetCode) (leetcode-cn.com)​​)

队列实现栈,栈实现队列,你学废了吗?_入栈_04

分析:

  • 不同于用栈实现队列,这里可以不需要两个队列实现栈,只需要一个队列即可。
  • 一个队列实现方法(针对于pop()方法的实现)
  • 只需要将队列的前length - 1个元素移到队列后面即可。
  • 直接shift()取出的第一个元素就是我们要的结果。

MyStack.prototype.pop = function() {
let k = this.queue.length - 1;
while(k-- > 0){
this.queue.push(this.queue.shift());
}
return this.queue.shift();
};

  • 两个队列实现方法(针对于pop()方法的实现)
  • 使用一个辅助队列,当辅助队列里的元素为空时我们将另外一个队列与辅助队列交换。
  • 将辅助队列的元素出队放到另外一个队列中,剩下一个元素即可,这里可以采用while循环,控制队列长度为1。
  • 最后直接将辅助队列剩下的元素取出即可,shift()。

MyStack.prototype.pop = function() {
if(!this.queue1.length){
[this.queue1, this.queue2] = [this.queue2, this.queue1];
}
while(this.queue1.length > 1){
this.queue2.push(this.queue1.shift());
}
return this.queue1.shift();
};

解答方法一:一个队列实现

var MyStack = function() {
this.queue = [];
};

MyStack.prototype.push = function(x) {
this.queue.push(x);
};

MyStack.prototype.pop = function() {
let k = this.queue.length - 1;
while(k-- > 0){
this.queue.push(this.queue.shift());
}
return this.queue.shift();
};

MyStack.prototype.top = function() {
const x = this.pop();
this.queue.push(x);
return x;
};

MyStack.prototype.empty = function() {
return !this.queue.length;
};

解答方法二:两个队列实现

var MyStack = function() {
this.queue1 = []; // 辅助队列1
this.queue2 = [];
};

MyStack.prototype.push = function(x) {
this.queue2.push(x);
};

MyStack.prototype.pop = function() {
if(!this.queue1.length){
[this.queue1, this.queue2] = [this.queue2, this.queue1];
}
while(this.queue1.length > 1){
this.queue2.push(this.queue1.shift());
}
return this.queue1.shift();
};

MyStack.prototype.top = function() {
const x = this.pop();
this.queue2.push(x);
return x;
};

MyStack.prototype.empty = function() {
return !this.queue2.length;
};


举报

相关推荐

0 条评论