232.用栈实现队列
题目链接/文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html
思路:
实现这个先入先出队列可以使用两个栈来模拟。一个栈用于入队操作,另一个栈用于出队操作。
步骤:
- 定义两个栈
stack1
和stack2
,其中stack1
用于入队操作,stack2
用于出队操作。 - 入队操作
push
:直接将元素x
压入stack1
。 - 出队操作
pop
:如果stack2
是空的,则将stack1
中的所有元素依次弹出并压入stack2
,此时stack2
的栈顶元素就是队列的开头元素,将其弹出即可。如果stack2
不为空,则直接弹出stack2
的栈顶元素。 - 返回队列开头元素
peek
:如果stack2
是空的,则将stack1
中的所有元素依次弹出并压入stack2
,此时stack2
的栈顶元素就是队列的开头元素,返回它。如果stack2
不为空,则直接返回stack2
的栈顶元素。 - 判断队列是否为空
empty
:当stack1
和stack2
都是空的时候,队列为空,返回true
,否则返回false
。
完整代码
import java.util.Stack;
class MyQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
/** Get the front element. */
public int peek() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push(1);
myQueue.push(2);
int peek = myQueue.peek();
System.out.println(peek); // 输出 1
int pop = myQueue.pop();
System.out.println(pop); // 输出 1
boolean empty = myQueue.empty();
System.out.println(empty); // 输出 false
}
}
225. 用队列实现栈
题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html
两个队列
思路:
用两个队列 queue1
和 queue2
。入栈操作直接将元素加入到 queue1
中,并更新 topElement
为最新的元素。出栈操作需要将 queue1
中的元素依次移动到 queue2
中,直到只剩下一个元素,将其弹出并返回。然后交换 queue1
和 queue2
的引用,使得 queue1
变为空队列,queue2
变为下一次出栈操作所需的队列。返回栈顶元素时直接返回 topElement
。判断栈是否为空只需要判断 queue1
是否为空。
整体思路就是通过两个队列来模拟栈的后入先出(LIFO)特性,每次入栈时都更新 topElement
为最新入栈的元素,并将所有元素移动到另一个队列中以保证栈顶元素在队列的最前面,这样出栈操作就可以按照栈的后入先出的顺序进行了。
完整代码:
import java.util.LinkedList;
import java.util.Queue;
class MyStack {
private Queue<Integer> queue1;
private Queue<Integer> queue2;
private int topElement;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue1.add(x);
topElement = x;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
while (queue1.size() > 1) {
topElement = queue1.remove();
queue2.add(topElement);
}
int poppedElement = queue1.remove();
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
return poppedElement;
}
/** Get the top element. */
public int top() {
return topElement;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty();
}
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
int top = myStack.top();
System.out.println(top); // 输出 2
int pop = myStack.pop();
System.out.println(pop); // 输出 2
boolean empty = myStack.empty();
System.out.println(empty); // 输出 false
}
}
一个队列
思路:
用一个队列 queue
来模拟栈的操作。入栈操作时,先将元素加入队列中,然后对队列中的前 size
个元素依次出队再入队,这样就保证了刚入队的元素在队列的最前面,相当于栈顶元素。出栈操作直接将队列中的元素出队即可。返回栈顶元素时直接返回队列的首个元素。判断栈是否为空只需要判断队列是否为空。通过这样的方式,我们可以用一个队列来实现栈的操作。
步骤:
- 使用一个队列
queue
来存储栈中的元素。 - 入栈操作时,先获取当前队列的大小
size
,然后将元素x
入队到队列中。接着对队列中的前size
个元素依次出队再入队,这样就保证了刚入队的元素在队列的最前面,相当于栈顶元素。 - 出栈操作时,直接将队列中的元素出队即可。
- 获取栈顶元素时,返回队列的首个元素。
- 判断栈是否为空时,判断队列是否为空。
完整代码:
import java.util.LinkedList;
import java.util.Queue;
class MyStack {
private Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
queue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
int size = queue.size();
queue.offer(x);
for (int i = 0; i < size; i++) {
queue.offer(queue.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
}
/** Get the top element. */
public int top() {
return queue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
int top = myStack.top();
System.out.println(top); // 输出 2
int pop = myStack.pop();
System.out.println(pop); // 输出 2
boolean empty = myStack.empty();
System.out.println(empty); // 输出 false
}
}