신규 블로그를 만들었습니다!
package queue;
public class CirculationQueue {
private final int SIZE = 10;
private Object[] buffer = new Object[SIZE];
private int tail = 0, head = 0;
public Object nextIndex(int index) {
return (index+1)%SIZE;
}
public void push(Object item) {
if ((tail + 1) % SIZE == head % SIZE) {
System.out.println("Queue is Full! ["+item+" can't push]");
} else {
buffer[tail] = item;
tail = (Integer)nextIndex(tail);
}
}
public Object pop() {
if (tail % SIZE == head % SIZE) {
System.out.println("Queue is Empty!");
return null;
} else {
Object result = buffer[head];
head = (Integer)nextIndex(head);
return result;
}
}
public static void main(String[] args) {
CirculationQueue queue = new CirculationQueue();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
queue.push(6);
queue.push(7);
queue.push(8);
System.out.println(queue.pop());
System.out.println(queue.pop());
queue.push(9);
queue.push(10);
queue.push(11);
queue.push(12);
queue.push(13);
queue.push(14);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
}
}
'취업 및 공부' 카테고리의 다른 글
공부 :: BFS 미로찾기 (JAVA) (1) | 2018.03.30 |
---|---|
공부 :: Queue 2개로 Stack 만들기 (JAVA) (1) | 2018.03.30 |
공부 :: Stack 2개를 이용해서 Queue 만들기 (자바/JAVA) (1) | 2018.03.29 |
공부 :: Stack / 스택 프로그래밍 (1) | 2018.03.29 |
공부 :: rank 알고리즘 (4) | 2018.03.29 |
최근댓글