**
Leetcode刷题 225题:用队列实现栈(基于Java和c++两种语言)
**
题目:
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空
注意:
你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
解题思路:
因为栈的特性是“先进后出”,即最后入栈的元素最先出栈,在使用队列实现栈时,应满足队列前端的元素是最后入栈的元素。可以使用两个队列实现栈的操作,其中queue1是用于存储栈内的除了首元素的其他元素,而queue2是用于存储进入栈内的首元素。
入栈push操作时,首先判断队列queue1是否为空,若为空,则将元素x压入队列1中,否则,则压入队列queue1中;
弹栈pop操作时,如果队列queue1为空,且又当队列queue2的大小大于1时,则将队列queue2移出的值压入队列queue1中,而且返回队列queue2移出的值;否则,当队列queue1的大小大于1时,则将队列queue1移出的值压入队列2中,返回队列queue1移出的值;
top取值操作时,重复上述的弹栈操作,最后将队列queue1的出队列的值重新压入在队列queue2中,所返回的值就是top所需要去的值。
原理图:
Java程序:
class MyStack {
/** Initialize your data structure here.
*队列的接口在Java中使用的是LinkList<>();
*对列中的添加元素方法应当使用offer(),add();
*队列中的移除方法应当使用poll(),remove();
*/
LinkedList<Integer> queue1 = new LinkedList<>();//定义队列1
LinkedList<Integer> queue2 = new LinkedList<>();//定义队列2
public MyStack() {
}
/** Push element x onto stack. */
public void push(int x) {
//首先判断队列1是否为空,若为空,则将x压入队列2中,否则,压入队列1中
if(queue1.isEmpty()){
queue2.offer(x);
}
else{
queue1.offer(x);
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
//如果队列1为空
if(queue1.isEmpty()){
//又当队列2的大小大于1时,则将队列2移出的值压入队列1中,返回队列2移出的值;
while(queue2.size()>1){
queue1.offer(queue2.poll());
}
return queue2.poll();
}
//否则,当队列1的大小大于1时,则将队列1移出的值压入队列2中,返回队列1移出的值。
else{
while(queue1.size()>1){
queue2.offer(queue1.poll());
}
return queue1.poll();
}
}
/** Get the top element. */
public int top() {
int top = 0;
if(queue1.isEmpty()){
while(queue2.size()>1){
queue1.offer(queue2.poll());
}
top = queue2.poll();
queue1.offer(top);
}else{
while(queue1.size()>1){
queue2.offer(queue1.poll());
}
top = queue1.poll();
queue2.offer(top);
}
return top;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty() && queue2.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
结果:
c++代码:
class MyStack {
public:
/** Initialize your data structure here. */
queue<int> base;
queue<int> pri;
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
base.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
while(base.size() > 1){
pri.push(base.front());
base.pop();
}
int x = base.front();
base.pop();
while(!pri.empty()){
base.push(pri.front());
pri.pop();
}
return x;
}
/** Get the top element. */
int top() {
while(base.size() > 1){
pri.push(base.front());
base.pop();
}
int x = base.front();
pri.push(base.front());
base.pop();
while(!pri.empty()){
base.push(pri.front());
pri.pop();
}
return x;
}
/** Returns whether the stack is empty. */
bool empty() {
if(!base.empty())
return false;
else
return true;
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
结果: