티스토리 뷰

큐를 2개 이용하여 스택처럼 사용


import java.util.LinkedList;
import java.util.Queue;
/**
* 큐를 이용하여 스택 구현하기
* 큐 2개를 이용하여 스택을 구현하는 방법
*
* push 시점에는 inBox 큐에 입력
* pop 시점에는 inBox 에 있는 데이터를
* outBox에 넣어서 출력한다.
*
* @author whitebeard
*
*/
public class QueueStack {
private Queue<Integer> inBox;
private Queue<Integer> outBox;
public QueueStack() {
inBox = new LinkedList<>();
outBox = new LinkedList<>();
}
public void push(int number) {
inBox.add(number);
}
public int pop() {
if(outBox.isEmpty()) {
while(!inBox.isEmpty()) {
outBox.add(inBox.poll());
}
}
return outBox.poll();
}
public static void main(String[] args) {
QueueStack stack = new QueueStack();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop());
stack.push(4);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
view raw QueueStack.java hosted with ❤ by GitHub




반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/04   »
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 29 30
글 보관함