- 思路
- 两个栈,入队一直用第一个栈,出队,把第一个栈数据倒转到第二栈,第二个栈的基本操作,就跟队列基本操作一致。
- 代码
public class MyQueue {
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
myQueue.push(4);
myQueue.push(5);
while (!myQueue.empty()) {
System.out.println(myQueue.pop());
}
}
Stack<Integer> stackIn= new Stack<>();;
Stack<Integer>stackIn2 =new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
if(stackIn.isEmpty()) {
while (!stackIn2.isEmpty()) {
stackIn.push(stackIn2.pop());
}
}
stackIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stackIn2.isEmpty()) {
while (!stackIn.isEmpty()) {
stackIn2.push(stackIn.pop());
}
}
return stackIn2.pop();
}
/** Get the front element. 改进 */
public int peek() {
if(stackIn2.isEmpty()) {
while (!stackIn.isEmpty()) {
stackIn2.push(stackIn.pop());
}
}
return stackIn2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
if(stackIn.isEmpty() && stackIn2.isEmpty()) {
return true;
}
return false;
}
}
网友评论