package 栈与队列上;
import java.util.Stack;
public class MyQueue {
public Stack stack1 = new Stack();
public Stack stack2 = new Stack();
public int size;
public void push(int x) {
stack1.push(x);
size++;
}
public void pop() {
if (!stack2.isEmpty()) {
stack2.pop();
} else {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
stack2.pop();
}
size--;
}
public int peek() {
if (!stack2.isEmpty()) {
return stack2.peek();
} else {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.peek();
}
}
public boolean empty() {
return size == 0 ? true : false;
}
public static void main(String[] args) {
MyQueue myqueue = new MyQueue();
System.out.println("队列初始大小:" + myqueue.size);
myqueue.push(1);
myqueue.push(2);
myqueue.push(3);
System.out.println("队列赋值后大小:" + myqueue.size);
while (!myqueue.empty()) {
System.out.print(myqueue.peek() + " ");
myqueue.pop();
}
}
}