public class TwoQueueForOneStack09 {
static class Stackck {
static Queue<Integer> queue1 = new LinkedList<>();
static Queue<Integer> queue2 = new LinkedList<>();
static public void push(int value){
if (queue1.size() <= 0 && queue2.size() <= 0) {
queue1.offer(value);
} else if (queue1.size() > 0) {
queue1.offer(value);
} else {
queue2.offer(value);
}
}
static public int pop(){
if (queue1.size() <= 0 && queue2.size() <= 0) {
return -1;
}
if (queue1.size() > 0) {
int count = queue1.size();
for (int i = 0; i < count - 1; i++) {
queue2.offer(queue1.poll());
}
return queue1.poll();
} else {
int count = queue2.size();
for (int i = 0; i < count - 1; i++) {
queue1.offer(queue2.poll());
}
return queue2.poll();
}
}
}
public static void main(String[] args) {
Stackck stackck = new Stackck();
stackck.push(1);
stackck.push(2);
stackck.push(3);
System.out.println(stackck.pop());
System.out.println(stackck.pop());
stackck.push(4);
System.out.println(stackck.pop());
stackck.push(5);
System.out.println(stackck.pop());
System.out.println(stackck.pop());
}
}