1、问题
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
2、示例
输入:["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []]
输出:[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
3、实现思路
(1)理解题意
(2)实现思路,按照正常的数组来即可,但是需要记住队和栈的特点
4、具体步骤
(1)创建栈:定义两个队列,其中一个用来处理插入、判空,另一个用来处理返回栈顶元素操作、弹出栈顶元素的操作
(2)push操作:插入,给queue1使用push插入即可
(3)pop弹出操作:栈是先进后出,队是先进先出,将队1的第一个弹出作为队2的输入,然后队2就是队1的逆序(队1剩的最后一个元素就是栈顶元素,即需要删除的元素),然后将剩余元素按照相同的方法返回队1
(4)top返回栈顶元素:和pop相似,只是不删除栈顶元素,使用临时变量存储,然后将队1剩余的元素压入队2,然后将队2所有的元素返回给队1
(5)empty判断栈是否为空(判断队1的长度即可)
5、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>用队列实现栈</title>
</head>
<body>
<p>
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
<p>
<h4>
实现 MyStack 类:
</h4>
void push(int x) 将元素 x 压入栈顶。<br>
int pop() 移除并返回栈顶元素。<br>
int top() 返回栈顶元素。<br>
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
</p>
</p>
<p>
输入:["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]<br>
输出:[null, null, null, 2, 2, false]
<p>
解释:<br>
MyStack myStack = new MyStack();<br>
myStack.push(1);<br>
myStack.push(2);<br>
myStack.top(); // 返回 2<br>
myStack.pop(); // 返回 2<br>
myStack.empty(); // 返回 False<br>
</p>
</p>
<script>
// 创建栈
var MyStack = function() {
this.queue1=[]
this.queue2=[]
};
/**
* @param {number} x
* @return {void}
*/
// 给栈中添加元素
MyStack.prototype.push = function(x) {
this.queue1.push(x)
};
/**
* @return {number}
*/
// 弹出(栈顶元素)(队1的最后一个元素)
MyStack.prototype.pop = function() {
// 栈是先进后出,队是先进先出,将队1的第一个弹出作为队2的输入,然后队2就是队1的逆序(队1剩的最后一个元素就是栈顶元素,即需要删除的元素),然后将剩余元素按照相同的方法返回队1
let topElement;
while (this.queue1.length > 1) {
this.queue2.push(this.queue1.shift())
}
topElement = this.queue1.shift()
while (this.queue2.length) {
this.queue1.push(this.queue2.shift());
}
return topElement;
};
/**
* @return {number}
*/
// 返回栈顶元素
MyStack.prototype.top = function() {
// 和pop相似,只是不删除栈顶元素,使用临时变量存储,然后将队1剩余的元素压入队2,然后将队2所有的元素返回给队1
let topElement;
while (this.queue1.length > 1) {
this.queue2.push(this.queue1.shift())
}
topElement = this.queue1[0]
this.queue2.push(this.queue1.shift())
while (this.queue2.length > 0) {
this.queue1.push(this.queue2.shift());
}
return topElement;
};
/**
* @return {boolean}
*/
// 判断栈是否为空(判断队1的长度即可)
MyStack.prototype.empty = function() {
return !this.queue1.length;
};
var myStack = new MyStack();
myStack.push(1);
myStack.push(2);
console.log(myStack.top()); // Output: 2
console.log(myStack.pop()); // Output: 2
console.log(myStack.empty()); // Output: false
/**
* Your MyStack object will be instantiated and called as such:
* var obj = new MyStack()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/
</script>
</body>
</html>
6、力扣通过代码
// 创建栈
var MyStack = function() {
this.queue1=[]
this.queue2=[]
};
/**
* @param {number} x
* @return {void}
*/
// 给栈中添加元素
MyStack.prototype.push = function(x) {
this.queue1.push(x)
};
/**
* @return {number}
*/
// 弹出
MyStack.prototype.pop = function() {
// 栈是先进后出,队是先进先出,将队1的第一个弹出作为队2的输入,然后队2就是队1的逆序(队的第一个元素就是栈顶元素,即需要删除的元素)
let topElement;
while (this.queue1.length > 1) {
this.queue2.push(this.queue1.shift())
}
topElement = this.queue1.shift()
while (this.queue2.length) {
this.queue1.push(this.queue2.shift());
}
return topElement;
};
/**
* @return {number}
*/
// 返回栈顶元素
MyStack.prototype.top = function() {
let topElement;
while (this.queue1.length > 1) {
this.queue2.push(this.queue1.shift())
}
topElement = this.queue1[0]
this.queue2.push(this.queue1.shift())
while (this.queue2.length > 0) {
this.queue1.push(this.queue2.shift());
}
return topElement;
};
/**
* @return {boolean}
*/
// 判断栈是否为空
MyStack.prototype.empty = function() {
return !this.queue1.length;
};