主要内容:
栈和队列转化
目录
- 栈实现队列
- 队列实现栈
一、栈实现队列
1、 通过两个栈来实现,栈可以改变数据的顺序,但是注意要把stack1中的所有元素压入栈中,才能执行删除操作。push操作直接加入stack1栈一即可,isEmpty要注意要两个栈都为空
**
* 栈实现队列
*
* 通过两个栈,栈可以改变方向
*/
class MyQueue {
private Stack<Integer> stack1; // 栈可以改变输出顺序
private Stack<Integer> stack2;
public MyQueue() {
this.stack1 = new Stack<>();
this.stack2 = new Stack<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
dumpstackIn();
return stack2.pop();
}
public int peek() {
dumpstackIn();
return stack2.peek();
}
public boolean empty() {
if(stack1.empty() && stack2.empty()){
return true;
}
return false;
}
public void dumpstackIn(){
if(!stack2.empty()) return;
while(!stack1.empty()){ // 将stack1栈全部压入到stack2栈中
Integer num = stack1.pop();
stack2.push(num);
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
二、队列实现栈
1、 两种办法:一种是用一个栈,把队尾元素pop()出去,则把前面的元素全添加到后面,第二种办法就是用两个队列,通过辅助队列,把之前的元素加入到新加入的元素后面,实现先进后出。
2、删除操作正常,push操作改变了,和栈实现队列恰好相反。还可以直接使用队列的方法,而实现栈的功能,对用户是透明的。
/ 一个队列实现栈
class MyStack1 {
private Queue<Integer> q1;
public MyStack1() {
this.q1 = new LinkedList();
}
public void push(int x) {
q1.add(x);
}
// 这里默认指的是删除队尾的元素,先进后出
public int pop() {
rePosition();
return q1.poll();
}
// 也可用peek替换
public int top() {
rePosition();
int result = q1.poll();
q1.add(result);
return result;
}
public boolean empty() {
return q1.isEmpty();
}
// 把第一个元素重新放回后面
public void rePosition(){
int size = q1.size();
size--; // TODO: 2024/5/27 细节要注意,不能把队尾的那个元素也删了
while(size-->0)
q1.add(q1.poll());
}
}
/**
* 通过两个队列来实现栈
*/
class MyStack {
Queue<Integer> queue1; // 和栈中保持一样元素的队列
Queue<Integer> queue2; // 辅助队列
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public void push(int x) {
queue2.offer(x); // 先放在辅助队列中
while (!queue1.isEmpty()){
queue2.offer(queue1.poll());// 再把之前的元素加入到队列2中
}
// TODO: 2024/5/27 交换后 队列2始终为空
Queue<Integer> queueTemp;
queueTemp = queue1;
queue1 = queue2;
queue2 = queueTemp; // 最后交换queue1和queue2,将元素都放到queue1中
}
public int pop() {
return queue1.poll(); // 因为queue1中的元素和栈中的保持一致,所以这个和下面两个的操作只看queue1即可
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}