题目和示例
225. 用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
["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
代码
双队列1:
放的时候正常放入,取出来的时候先把dataQueue中的除了最后一个添加进来的所有元素放到tmpQueue队列中,然后通过poll()获得目的值,最后再通过交换两个队列将其他元素移回去dataQueue。
class MyStack {
// 存储数据的队列
Queue<Integer> dataQueue = new LinkedList<>();
// 临时队列
Queue<Integer> tmpQueue = new LinkedList<>();
// 记录栈顶元素
int top;
/**
* Push element x onto stack.
*/
public void push(int x) {
dataQueue.add(x);
top = x;
}
/**
* Removes the element on top of the stack and returns that element.
*/
public int pop() {
while (dataQueue.size() > 1) {
top = dataQueue.poll();
tmpQueue.add(top);
}
int num = dataQueue.poll();
// 再将dataQueue元素移回去
Queue tmp = dataQueue;
dataQueue = tmpQueue;
tmpQueue = tmp;
return num;
}
/**
* Get the top element.
*/
public int top() {
return top;
}
/**
* Returns whether the stack is empty.
*/
public boolean empty() {
return dataQueue.size() == 0;
}
}
/**
* 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();
*/
双队列2:
a作为一个辅助队列, 放的时候保证了b队列始终是正常队列的相反顺序。
class MyStack {
private Queue<Integer> a;//输入队列
private Queue<Integer> b;//输出队列
public MyStack() {
a = new LinkedList<>();
b = new LinkedList<>();
}
public void push(int x) {
a.offer(x);
// 将b队列中元素全部转给a队列
while(!b.isEmpty())
a.offer(b.poll());
// 交换a和b,使得a队列没有在push()的时候始终为空队列
Queue temp = a;
a = b;
b = temp;
}
public int pop() {
return b.poll();
}
public int top() {
return b.peek();
}
public boolean empty() {
return b.isEmpty();
}
}
单队列:
取的时候把除了最后一个元素的所有元素重新添加到队列中去。
class MyStack {
// 存储数据的队列
Queue<Integer> dataQueue = new LinkedList<>();
// 记录栈顶元素
int top;
/**
* Push element x onto stack.
*/
public void push(int x) {
dataQueue.add(x);
top = x;
}
/**
* Removes the element on top of the stack and returns that element.
*/
public int pop() {
int cnt = 0;
while (cnt < dataQueue.size() -1) {
cnt++;
top = dataQueue.poll();
dataQueue.add(top);
}
return dataQueue.poll();
}
/**
* Get the top element.
*/
public int top() {
return top;
}
/**
* Returns whether the stack is empty.
*/
public boolean empty() {
return dataQueue.size() == 0;
}
}
/**
* 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();
*/
用数组实现栈
public class MyStackByArray {
// 存储数据
int[] array;
// 最大容量
int maxSize;
// 实际存储大小
int top;
public MyStackByArray(int size) {
maxSize = size;
array = new int[size];
top = -1;
}
public void push(int value) {
if (top < maxSize - 1) {
top++;
array[top] = value;
}
}
public int pop() {
int result = array[top];
top--;
return result;
}
public int peek() {
return array[top];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == (maxSize - 1);
}
public static void main(String[] args) {
MyStackByArray stack = new MyStackByArray(3);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.isFull());
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.isEmpty());
}
}