两个栈实现一个队列

​ 本文使用栈的基本操作实现队列的效果,由于栈的LIFO,两个栈逆逆得顺就能FIFO

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class  extends Number> {
Stack<T> data;
Stack<T> tempData;

MyQueue(){
this.data=new Stack<T>();
this.tempData=new Stack<T>();
}

void add(T e){
data.push(e);
}


* 两个栈实现对队列的消费操作
*/
T remove(){
//放入临时栈
while (!data.empty())
tempData.push(data.pop());
//去掉栈顶元素
T t= tempData.pop();
//从临时栈倒回去
while (!tempData.empty())
data.push(tempData.pop());
return t;
}
}

效果

1
2
3
4
5
6
7
8
9
10
11
12
MyQueue queue = new MyQueue();
queue.add(3);
queue.add(4);
queue.add(5);
queue.add(6);
queue.add(7);

System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());

运行结果: