一、使用两个栈模拟队列的pop和push操作
package com
.digigd
.cloud
.dolphin
.digigd
.pub
.test
;
import java
.util
.Stack
;
public class QueuePushPop {
Stack
<Integer> stack1
= new Stack<Integer>();
Stack
<Integer> stack2
= new Stack<Integer>();
public void push(Integer num
) {
stack1
.push(num
);
}
public int pop() {
Integer re
= null
;
if (!stack2
.empty()) {
re
= stack2
.pop();
} else {
if (!stack1
.empty()) {
while (!stack1
.empty()) {
re
= stack1
.pop();
stack2
.push(re
);
}
if (!stack2
.empty()) {
re
= stack2
.pop();
}
}
}
return re
;
}
}
二、两个队列模拟栈的pop和push操作
在这里插入代码片
转载请注明原文地址: https://win8.8miu.com/read-16193.html