用两个栈实现一个队列,包括入队,出队,弹出队首元素,判断队列是否为空功能。
import java.util.Stack;
public class StackToQueue<T> {
private Stack<T> s1 = new Stack<>();
private Stack<T> s2 = new Stack<>();
//入队
public void offer(T val){
this.s1.push(val);
}
//出队
public T poll(){
if (isEmpty()){
throw new RuntimeException("队列为空");
}
if (this.s2.isEmpty()){
while(!this.s1.isEmpty()){
T val = this.s1.pop();
this.s2.push(val);
}
}
return this.s2.pop();
}
//弹出队首元素
public T peek(){
if (isEmpty()){
throw new RuntimeException("队列为空");
}
if (this.s2.isEmpty()){
while(!this.s1.isEmpty()){
T val = this.s1.pop();
this.s2.push(val);
}
}
return this.s2.peek();
}
//队列是否为空
public boolean isEmpty(){
return s1.isEmpty() && s2.isEmpty();
}
}
来看看测试:
public class TestDemo4 {
public static void main(String[] args) {
StackToQueue<Integer> queue = new StackToQueue<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
System.out.println(queue.peek());
System.out.println(queue.isEmpty());
queue.poll();
queue.poll();
queue.poll();
System.out.println(queue.isEmpty());
queue.poll();
}
}
符合预期结果。
用一个队列实现一个栈,包括入栈,出栈,弹出栈顶元素,判断栈是否为空功能
import java.util.LinkedList;
import java.util.Queue;
public class QueueToStack1<T> {
private Queue<T> queue = new LinkedList<>();
//入栈
public void push(T val){
this.queue.offer(val);
}
//出栈
public T pop(){
if (isEmpty()){
throw new RuntimeException("栈为空");
}
int size = this.queue.size();
for (int i = 0; i < size-1; i++) {
T val = this.queue.poll();
this.queue.offer(val);
}
return this.queue.poll();
}
//弹出栈顶元素
public T peek(){
if (isEmpty()){
throw new RuntimeException("栈为空");
}
int size = this.queue.size();
for (int i = 0; i < size-1; i++) {
T val = this.queue.poll();
this.queue.offer(val);
}
T res = this.queue.poll();
this.queue.offer(res);
return res;
}
//判断栈是否为空
public boolean isEmpty(){
return this.queue.isEmpty();
}
}
来看看测试:
public class TestDemo5 {
public static void main(String[] args) {
QueueToStack1<Integer> s = new QueueToStack1<>();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.peek());
System.out.println(s.isEmpty());
s.pop();
s.pop();
s.pop();
System.out.println(s.isEmpty());
System.out.println(s.peek());
}
}
符合预期结果。
用两个队列实现一个栈,包括入栈,出栈,弹出栈顶元素,判断栈是否为空功能
import java.util.LinkedList;
import java.util.Queue;
public class QueueToStack2 <T>{
private Queue<T> q1 = new LinkedList<>();
private Queue<T> q2 = new LinkedList<>();
//入栈
public void push(T val){
if (!q2.isEmpty() && q1.isEmpty()){
q1.offer(val);
while(!q2.isEmpty()){
T res = q2.poll();
q1.offer(res);
}
}else if(q2.isEmpty() && !q1.isEmpty()){
q2.offer(val);
while(!q1.isEmpty()){
T res = q1.poll();
q2.offer(res);
}
}else if(q2.isEmpty() && q1.isEmpty()){
q1.offer(val);
}
}
//出栈
public T pop(){
if (isEmpty()){
throw new RuntimeException("栈为空");
}else if (!this.q1.isEmpty()){
return this.q1.poll();
}else{
return this.q2.poll();
}
}
//弹出栈顶元素
public T peek(){
if (this.q1.isEmpty() && this.q2.isEmpty()){
throw new RuntimeException("栈为空");
}else if (!this.q1.isEmpty()){
return this.q1.peek();
}else{
return this.q2.peek();
}
}
//判断栈是否为空
public boolean isEmpty(){
return this.q1.isEmpty() && this.q2.isEmpty();
}
}
来看看测试:
public class TestDemo6 {
public static void main(String[] args) {
QueueToStack2<Integer> s = new QueueToStack2<>();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.peek());
System.out.println(s.isEmpty());
s.pop();
s.pop();
s.pop();
System.out.println(s.isEmpty());
s.peek();
}
}
符合预期结果。